Multiple a matrix by every column of another matrix and stack the results in some form
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
Voss
2022년 3월 14일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!