【问题标题】:Adding a row vector and a column vector添加行向量和列向量
【发布时间】:2017-08-10 12:56:12
【问题描述】:

我需要在 MATLAB 中添加两个具有不同维度的变量。

A 的尺寸为 1*60

B 的尺寸为 60*1

由于矩阵维度不同,我无法使用 sum 命令对它们求和。请问有没有办法添加呢?

【问题讨论】:

    标签: matlab vector sum


    【解决方案1】:

    使用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(:);
    

    【讨论】:

      猜你喜欢
      • 2018-09-24
      • 2012-03-15
      • 2013-06-29
      • 1970-01-01
      • 2022-06-10
      • 2022-11-25
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多