# subtracting columns of one matrix from the other and chaining the awnsers

1 view (last 30 days)
Yuval Shekel on 24 Nov 2021
Answered: Image Analyst on 24 Nov 2021
i have a matrix A=[1,2,3; 4,5,6 ; 7,8,9] and matrix B=[10,20,30,40; 10,20,30,40; 10,20,30,40]
i want to substract A(:,1)-B and chain it to A(:,2)-B and so on.
without using a loop!
any ideas??

Image Analyst on 24 Nov 2021
What is your definition of "chain"? Do you mean concatenate?
A(:, 1) is a column vector while B is a matrix. Were you planning on using automatic expansion?
A=[1,2,3; 4,5,6 ; 7,8,9]
A = 3×3
1 2 3 4 5 6 7 8 9
B=[10,20,30,40; 10,20,30,40; 10,20,30,40]
B = 3×4
10 20 30 40 10 20 30 40 10 20 30 40
% "i want to substract A(:,1)-B and chain it to A(:,2)-B and so"
m1 = A(:,1) - B
m1 = 3×4
-9 -19 -29 -39 -6 -16 -26 -36 -3 -13 -23 -33
m2 = A(:, 2) - B
m2 = 3×4
-8 -18 -28 -38 -5 -15 -25 -35 -2 -12 -22 -32
% Concatenate m1 to the right of m2
output = [m2, m1]
output = 3×8
-8 -18 -28 -38 -9 -19 -29 -39 -5 -15 -25 -35 -6 -16 -26 -36 -2 -12 -22 -32 -3 -13 -23 -33

R2021b

### Community Treasure Hunt

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

Start Hunting!