【发布时间】:2017-08-10 12:56:12
【问题描述】:
我需要在 MATLAB 中添加两个具有不同维度的变量。
A 的尺寸为 1*60
B 的尺寸为 60*1
由于矩阵维度不同,我无法使用 sum 命令对它们求和。请问有没有办法添加呢?
【问题讨论】:
我需要在 MATLAB 中添加两个具有不同维度的变量。
A 的尺寸为 1*60
B 的尺寸为 60*1
由于矩阵维度不同,我无法使用 sum 命令对它们求和。请问有没有办法添加呢?
【问题讨论】:
使用transpose function .' 或colon operator (:)
不要包含这两行代码,它们只是为这个例子设置的:
A = ones(1, 60); % create an arbitrary row vector 1x60
B = ones(60, 1); % create an arbitrary column vector 60x1
从这些选项中选择一个,每个选项上方的注释描述了它的作用。
% output a vector the same orientation as A
C = A + B.';
% output a vector the same orientation as B
C = A.' + B;
% output a column vector, no matter the orientation of A and B
% Ensure that they are vectors, this will give undesired results if A and B are 2D.
C = A(:) + B(:);
【讨论】: