필터 지우기
필터 지우기

Multiply a "stack" of matrices with a "stack" of vectors

조회 수: 4 (최근 30일)
Michael
Michael 2014년 3월 4일
댓글: James Tursa 2014년 3월 7일
I have a matrix 2 x 2 x n and a vector 2 x n. So basically, I have n layers of 2 x 2 matrices and n layers of vectors with 2 entries. I d like to do a matrix-vector multiplication on each layer.
I could easily do this using a for loop, but is there a way which directly supports such an operation?

채택된 답변

Jos (10584)
Jos (10584) 2014년 3월 4일
편집: Jos (10584) 2014년 3월 7일
One option:
n = 6
% M is your 2x2xn array of "matrices
M = rand(2,2,n)
% V is your 2xn array of "vectors"
V = rand(2,n)
fh = @(k) M(:,:,k) * V(:,k)
C = arrayfun(fh,1:n,'un',0)
C2 = cat(3,C{:})
Corrected.
  댓글 수: 3
Jos (10584)
Jos (10584) 2014년 3월 7일
You did ask for " a way which directly supports such an operation ", not for a faster way! ;-)
To be honest, I think a straight forward for-loop is as good as the accumarray approach in this case.
James Tursa
James Tursa 2014년 3월 7일
If you want speed, I would suggest looking at one of the solutions I have posted.

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

추가 답변 (1개)

James Tursa
James Tursa 2014년 3월 4일
편집: James Tursa 2014년 3월 7일

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by