How to multiply Multidimensional Arrays with a column vector

조회 수: 15 (최근 30일)
Tristan
Tristan 2013년 10월 28일
댓글: Shambhavi Singh 2019년 2월 14일
I want to multiply A with B so that C(:,:,1) is equal to A(:,:,1)*B(1) and C(:,:,2) is equal to A(:,:,2)*B(2)
>> A = cat(3, [2 8; 0 5], [1 3; 7 9])
A(:,:,1) =
2 8
0 5
A(:,:,2) =
1 3
7 9
>> B=[1 2]'
B =
1
2
I'm looking to get this:
>> C=???
C(:,:,1) =
2 8
0 5
C(:,:,2) =
2 6
14 18

답변 (3개)

sixwwwwww
sixwwwwww 2013년 10월 28일
편집: sixwwwwww 2013년 10월 28일
Dear Tristan, here is the code which performs the task:
A = cat(3, [2 8; 0 5], [1 3; 7 9]);
B=[1 2]';
for i = 1:length(B)
C(:, :, i) = B(i) * A(:, :, i);
end
disp(C)
I hope it helps. Good luck!
  댓글 수: 5
sixwwwwww
sixwwwwww 2013년 10월 28일
You can do it like this:
C = cat(3, [], [], B);
Shambhavi Singh
Shambhavi Singh 2019년 2월 14일
This should work too
C=A.*permute(B,[3 2 1]),3);
Permute switches the rows in B (So the elements of a column vector) with the third dimension

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2013년 10월 28일

Pavel Chmelar
Pavel Chmelar 2016년 1월 12일
Clear Matlab solution according sixwwwwww and Tristan:
B=zeros(1,1,2);
B(:)=[1,2];
C=bsxfun(@times,A,B);

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by