필터 지우기
필터 지우기

multiprod implementation question for matrix multiplication optimization

조회 수: 7 (최근 30일)
Daniel
Daniel 2016년 2월 11일
댓글: Daniel 2016년 2월 13일
Hi all. I'm trying to optimize matrix multiplication. What I am trying to do is take a stack of 3 x 3 matrices and multiply each matrix by one vector from an array of vectors .
I did find an implementation not using multiprod that solves this issue without for loops, but makes extensive use of repmat and performs worse than if I were to just use a for loop.
So for the case below: multiprod(A,B) would return [1 4 7] and [12 15 18].
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15; 16 17 18;]
B = [1 0; 0 0; 0 1]
Currently, multiprod assumes that you want to multiply each 3x3 by the 3x2 and as a result I get extraneous data. Adding the extra parameters about the inner dimensions to the function call did not work either.
So I guess my question is...can multiprod solve this issue and I'm just not implementing it correctly or am I better off using for loops?

채택된 답변

Matt J
Matt J 2016년 2월 11일
편집: Matt J 2016년 2월 11일
If we reshape your data slightly, we can use MTIMESX ( Download )
>> A = cat(3,[1 2 3; 4 5 6; 7 8 9],[ 10 11 12; 13 14 15; 16 17 18])
A(:,:,1) =
1 2 3
4 5 6
7 8 9
A(:,:,2) =
10 11 12
13 14 15
16 17 18
>> B=reshape(B,3,1,[])
B(:,:,1) =
1
0
0
B(:,:,2) =
0
0
1
>> mtimesx(A,B)
ans(:,:,1) =
1
4
7
ans(:,:,2) =
12
15
18

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by