Multiple a matrix by every column of another matrix and stack the results in some form
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Deepayan Bhadra
el 14 de Mzo. de 2022
Respondida: Voss
el 14 de Mzo. de 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 comentarios
Respuesta aceptada
Voss
el 14 de Mzo. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!