Multiple a matrix by every column of another matrix and stack the results in some form
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Deepayan Bhadra
am 14 Mär. 2022
Beantwortet: Voss
am 14 Mär. 2022
Hello, I have a matrix A: 3xn and a matrix B: 3x6. What I wish to do is subtract each column of B from A as follows:
B_11 is subtracted from A_1i for i = 1:n, B_21 is subtracted from A_2i for i = 1:n, B_31 is subtracted from A_3i for i = 1:n.
This is repeated for each column of B with the whole A matrix and the results stacked each time, giving a resultant matrix that's 18xn.
Or storing the 3xn matrices for the 6 different B columns in a structure is also okay.
I have tried combinations of repmat but nothing useful yet. Can we do this without a loop, preferably?
0 Kommentare
Akzeptierte Antwort
Voss
am 14 Mär. 2022
I think this will do it:
n = 10;
A = 100+2*reshape(1:3*n,3,[])
B = reshape(1:3*6,3,[])
new_A = repmat(A,size(B,2),1);
new_B = repmat(B(:),1,size(A,2));
result = new_A-new_B;
disp(result);
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!