Matrices multiplication in a loop
이전 댓글 표시
Hello
I have many matrices in two categories say M_i and N_j generated in a for loop. They all have order 2*2. I want to conv each M_i with every N_j i.e.
conv(M_1, N_1)
conv(M_1, N_2) ...
then:
conv(M_2, N_1)
conv(M_2, N_2)...
Any help?
댓글 수: 5
Rik
2019년 7월 25일
Why did you use numbered variables? This would be trivial with a nested loop if you had stored them in a cell array.
The best solution is going one step back and not use numbered variables.
Stephen23
2019년 7월 25일
"I have many matrices ... say M_i and N_j generated in a for loop..."
And that is the bad design decision right there.
If you had simply used indexing (in an ND numeric array or a cell array), then resolving your question would be trivial (hint: by also using indexing). But by creating lots of separate matrices, you will force yourself into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know some of the reasons why:
https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
In contrast, indexing is neat, simple, easy to understand, easy to debug, and very efficient (unlike what you are trying to do). You should use indexing.
TADA
2019년 7월 25일
doesn't conv accept only 1 dimentional vectors?
Also, if you instantiate 2 3D matrices instead of ixj 2D matrices your life would be easier:
% preallocate:
N = zeros(2,2,n);
M = zeros(2,2,m);
% where n and m are the number of matrices you instantiate today using i and j
% assuming that m ~= n:
for i = 1:n
% N(:,:,i) = whatever calculation you do today
end
for j = 1:m
% M(:,:,j) = whatever calculation you do today
end
tanveer haq
2019년 7월 25일
tanveer haq
2019년 7월 25일
편집: tanveer haq
2019년 7월 25일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!