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 comentarios

Rik
Rik el 25 de Jul. de 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
Stephen23 el 25 de Jul. de 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:
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
TADA el 25 de Jul. de 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
tanveer haq el 25 de Jul. de 2019
I am new to matlab. Therefore, kindly, write a small code for this idea if possible.
tanveer haq
tanveer haq el 25 de Jul. de 2019
Editada: tanveer haq el 25 de Jul. de 2019
After a long try I succed. Thanks to all of you.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2017b

Preguntada:

el 25 de Jul. de 2019

Editada:

el 25 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by