Matrices multiplication in a loop
Mostra commenti meno recenti
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 Commenti
Rik
il 25 Lug 2019
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
il 25 Lug 2019
"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
il 25 Lug 2019
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
il 25 Lug 2019
tanveer haq
il 25 Lug 2019
Modificato: tanveer haq
il 25 Lug 2019
Risposte (0)
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!