Reputation: 155
I need to add two variables which have different dimensions in MATLAB.
A
has dimensions 1*60
B
has dimensions 60*1
Since the matrix dimensions are not same, I cannot sum them by using the sum command. I would like to ask if there is any way to add them?
Upvotes: 1
Views: 1447
Reputation: 30046
Using the transpose function .'
or the colon operator (:)
Do not include these two lines of code, they are just set-up for this example:
A = ones(1, 60); % create an arbitrary row vector 1x60
B = ones(60, 1); % create an arbitrary column vector 60x1
Choose one of these options, a comment above each describes what it does.
% 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(:);
Upvotes: 3