A row vector and a column vector have compatible sizes. If you add a 1-by-3 vector to a 2-by-1 vector, then each vector implicitly expands into a 2-by-3 matrix before MATLAB executes the element-wise addition.
x = [1 2 3]x = 1 2 3y = [10; 15]y = 10 15x + yans = 11 12 13 16 17 18
If the sizes of the two operands are incompatible, then you get an error.
A = [8 1 6; 3 5 7; 4 9 2]A = 8 1 6 3 5 7 4 9 2m = [2 4]m = 2 4A - m
Matrix dimensions must agree.
This is from the MATLAB "Array vs. Matrix Operations page". Why does the second example output an error while the first doesn't? I see that the second example says that "matrix dimensions must agree", but why did that error not occur for the first example? A further explanation of this would be great. Thank you!
Best Answer