Subtraction between a matrix and a column vector

2 views (last 30 days)
So I have a matrix of the size 23x15 and a column vector of size 23x1. I need to subtract element 1 of the colum vector from the 1st row of the matrix, element 2 of the column vector from 2nd row of the matrix, and so on. Any suggestions about how I can do it?

Accepted Answer

Riccardo Scorretti
Riccardo Scorretti on 5 Apr 2022
Hi. If the matrix and the vector have compatible size (= like you mentioned), it is enough to use the minus operator:
A = rand(23,15);
x = rand(23,1);
A-x
ans = 23×15
0.2139 -0.4738 -0.0324 0.2879 -0.5164 -0.5204 -0.2573 -0.1252 -0.6030 0.0119 -0.5568 0.0707 0.1965 -0.2185 -0.3107 -0.3026 0.3745 -0.4007 -0.0694 -0.2514 -0.0244 -0.0126 -0.4829 0.1296 0.3859 0.1160 0.1024 -0.0164 -0.3397 -0.3895 -0.0364 -0.2650 -0.0361 -0.1169 -0.1134 0.6802 0.5602 0.0069 0.1628 -0.2122 -0.0523 0.5581 0.6691 0.0580 0.4566 -0.8499 -0.7361 -0.4907 -0.7858 -0.1153 -0.6816 -0.5459 -0.0587 -0.5956 -0.2459 -0.4201 -0.8757 -0.8312 -0.2901 -0.7761 -0.1458 0.3076 0.4151 0.4332 -0.2664 0.0339 -0.4702 0.4185 -0.3688 -0.0325 -0.3356 0.3817 0.0343 0.1336 -0.4908 -0.1485 -0.5956 -0.1521 -0.2995 -0.7787 -0.1461 -0.6266 -0.3964 -0.6241 -0.7282 -0.4468 -0.6754 -0.8928 -0.5799 -0.8771 -0.8125 0.1077 -0.6755 -0.7532 -0.2350 0.1244 -0.8128 -0.2916 -0.5226 -0.2108 -0.3767 -0.0239 -0.7943 -0.7744 -0.4538 -0.2816 -0.3594 -0.8926 -0.2823 -0.2469 -0.5546 0.0076 -0.3071 -0.8171 -0.3084 -0.6816 -0.9024 -0.6121 0.0152 -0.8946 0.1406 0.3451 0.3059 -0.2132 0.3232 -0.3814 0.1598 -0.4034 -0.1477 -0.2525 -0.4202 -0.4624 -0.1376 -0.5925 0.3209 0.6027 0.5138 0.2264 0.0285 -0.1912 -0.1324 0.5262 0.6078 -0.0720 0.4299 0.0051 0.4744 0.0725 -0.0732 0.4045
  1 Comment
Akshay Vivek Panchwagh
Akshay Vivek Panchwagh on 12 Apr 2022
Thank you. I actually needed to repeat the other matrix (the "x" one mentioned by you here). So I did the operation using 'repmat' and it worked.

Sign in to comment.

More Answers (1)

Torsten
Torsten on 5 Apr 2022
B = A - x
where A is your matrix and x is your vector.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!