As Jon pointed out, you can subtract two matrices if they are "the same size (same number of rows and columns)"
It is also possible to subtract a scalar (1x1 matrix) from any matrix:
>> A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
>> size(A)
ans =
3 2
>> s = -10;
>> S = A - s
S =
11 12
13 14
15 16
Also, it is sometimes possible to subtract a vector from a matrix:
>> B
B =
0 -10
>> size(B)
ans =
1 2
>> C = A - B
C =
1 12
3 14
5 16
>> size(C)
ans =
3 2
Or, subtract a vector from a matrix like this:
>> BB = -[ 10 20 30]'
BB =
-10
-20
-30
>> size(BB)
ans =
3 1
>> CC = A - BB
CC =
11 12
23 24
35 36
>> size(CC)
ans =
3 2
6 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1276443
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1276443
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1276718
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1276718
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1277988
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1277988
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1278038
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1278038
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1278858
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1278858
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1278973
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/722993-matrix-dimensions-must-agree#comment_1278973
Sign in to comment.