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 Comments

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

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017b

Asked:

on 25 Jul 2019

Edited:

on 25 Jul 2019

Community Treasure Hunt

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

Start Hunting!