multiply two series of vector in loops

조회 수: 1 (최근 30일)
Sylvain Bart
Sylvain Bart 2019년 9월 12일
댓글: Sylvain Bart 2019년 9월 12일
Hello, I have 1485 versions of a vector (41) so a matrix of 1485x41 and I have two matrix like this, so MA=1485x41 and MB=1485x41
I would like to multiply all the vectors (41) possibility to get a final Matrix of 2205225x41. I tried with loops but failed.
Here is my code:
% MA=1485x41 and MB=1485x41
vectorA=cell(size(MA,1),1); %index vector in a cell array
vectorB=cell(size(MB,1),1); %index vector in a cell array
solution=nan(length(MA(:,1)).*length(MB(:,1)), length(t)); %t=41 here is the final matrix that I would need
for i =1 : size(chemACIm,1)
for j=1:size(chemBCIm,1)
vectorA{i}=MA(i,:);
vectorB{j}=MB(j,:);
output(i, j, :)=MA(i,:).*MB(j,:); %
end
end
Thank you in advance for your answer,
Sylvain

채택된 답변

Guillaume
Guillaume 2019년 9월 12일
Assuming R2016b or later:
result = MA .* permute(MB, [3, 2, 1]);
will give you a 3D matrix, where result(i, :, j) is MA(i, :) .* MB(j, :)
I would suggest you keep the result like that, but if you really want a 2D matrix, then:
result = reshape(permute(MA .* permute(MB, [3, 2, 1]), [1, 3, 2]), [], size(MA, 2))
  댓글 수: 1
Sylvain Bart
Sylvain Bart 2019년 9월 12일
Many thanks for your answer, it save my time and my code is shorter that way

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

추가 답변 (0개)

카테고리

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