Multi Dimension Matrix Elements multiplication
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello,
I am strugling with some matrix multiplication...
I am having one M:
matrix and one N:
matrix with some given elements. Elements of M are given as
whereas elements of N are given as
. I wish to obtain maxtrix P:
whose elements
,
and
. Thus, matrix
would be
in my case.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529984/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529989/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529994/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/529999/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/530024/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/530004/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/530009/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/530014/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/530019/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/530024/image.png)
Does anyone know how to perform this complex operation in the fastest way (without loops)?
Thanks in advance
댓글 수: 0
답변 (2개)
Rik
2021년 2월 24일
Not completely without loops, but this should be fast enough. You should also be aware that loops can sometimes be the fastest way.
M=rand(3,2);N=rand(2,3,4);
%prime the execution engine to produce a more reliable comparison
timeit(@()option1(M,N));
timeit(@()option1(M,N))
timeit(@()option2(M,N))
isequal(option1(M,N),option2(M,N))
function P=option1(M,N)
P=zeros(size(M,1),size(N,2),size(N,3));
for n=1:size(M,1)
P(n,:,:)=M(n,1)*N(1,:,:)+M(n,2)*N(2,:,:);
end
end
function P=option2(M,N)
P=zeros(size(M,1),size(N,2),size(N,3));
for i=1:size(M,1)
for j=1:size(N,2)
for k=1:size(N,3)
P(i,j,k)=M(i,1)*N(1,j,k)+M(i,2)*N(2,j,k);
end
end
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!