Is it possible to multiply a 3D matrix with a coumn vector?

조회 수: 16 (최근 30일)
Vipin  Padinjarath
Vipin Padinjarath 2017년 3월 31일
댓글: Roman Gorlov 2021년 1월 28일
I have a 3D matrix with three rows and three columns. I want to multiply this matrix with a column vector of 3 rows. How can it be done?

답변 (4개)

Jan
Jan 2017년 3월 31일
편집: Jan 2017년 3월 31일
With Matlab >= 2016b:
A = rand(3, 3, 1000);
b = rand(3, 1);
C = squeeze(sum(A .* reshape(b, 1, 3), 2));
With older versions:
C = squeeze(sum(bsxfun(@times, A, reshape(b, 1, 3)), 2))
  댓글 수: 3
Jan
Jan 2017년 3월 31일
편집: Jan 2017년 3월 31일
The three versions have advantages and disadvantages:
  • KSSV: This is clean and simple. During debugging the intention is directly clear. The loop will need some time.
  • Andrei: This is efficient because it uses the fast built-in matrix multiplication. For large inputs permute needs time.
  • Mine: This does need a copy of the input data, but a temporary array also before creating the sum.
Timings:
A = rand(3, 3, 1e6);
B = rand(3, 1);
tic, C = zeros(3, size(A, 3));
for i = 1:size(A, 3)
C(:,i) = A(:,:,i)*B ;
end, toc
tic; C = reshape(reshape(permute(A,[2,1,3]),3,[]).'*B,3,[]); toc
tic; C = squeeze(sum(bsxfun(@times, A, reshape(B, 1, 3)), 2)); toc
Elapsed time is 1.605281 seconds. % Loop
Elapsed time is 0.069983 seconds. % permute
Elapsed time is 0.098582 seconds. % sum(times())
(Matlab 2009a! Test this on a modern version also)
I would insert KSSV's loop as a comment and Andrei's method for computations.
Richard
Richard 2020년 2월 19일
A = rand(3, 3, 1e6);
B = rand(3, 1);
tic, C = zeros(3, size(A, 3));
for i = 1:size(A, 3)
C(:,i) = A(:,:,i)*B ;
end, toc
tic; C = reshape(reshape(permute(A,[2,1,3]),3,[]).'*B,3,[]); toc
tic; C = squeeze(sum(bsxfun(@times, A, reshape(B, 1, 3)), 2)); toc
Elapsed time is 0.891301 seconds. % Loop
Elapsed time is 0.082350 seconds. % permute
Elapsed time is 0.088938 seconds. % sum(times())
Matlab 2019b Update 3

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


Andrei Bobrov
Andrei Bobrov 2017년 3월 31일
C = reshape(reshape(permute(A,[2,1,3]),3,[]).'*B,3,[]);

KSSV
KSSV 2017년 3월 31일
A = rand(3,3,3) ;
B = rand(3,1) ;
C = zeros(3,3) ;
for i = 1:3
C(:,i) = A(:,:,i)*B ;
end

Tyler R
Tyler R 2017년 5월 26일
편집: Tyler R 2017년 5월 26일
I got confused because some of the dimensions are size 3, but there is also a 3rd dimension, so for generalization's sake:
N = 150;
K = 20;
T = 30;
A = rand(N,K,T);
B = rand(K,1);
C = zeros(N,T);
Andrei's method:
C = reshape(reshape(permute(A,[2 1 3]),K,[]).'*B,N,[]);
Jan's method:
C = squeeze(sum(A .* reshape(B, 1, K), 2));
KSSV's method:
for t = 1:T
C(:,t) = A(:,:,t)*B ;
end
  댓글 수: 1
Roman Gorlov
Roman Gorlov 2021년 1월 28일
When B is rand(K, P), with P > 1, then both proposed methods don't work. I've tried different permuations, but no luck replicating the for loop result.

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

카테고리

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