Multiple a matrix by every column of another matrix and stack the results in some form
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Voss
il 14 Mar 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!