Nested for loop for multipication

1 vue (au cours des 30 derniers jours)
lilly lord
lilly lord le 14 Sep 2021
Commenté : lilly lord le 14 Sep 2021
Hello I am trying to multiply columns of Matrix E with numbers 1 till 5. Columns are taken one by one named as G. When I multiply G=E(:,1)=[0;1] I got the correct answer . If I take G one by one (without j for loop I got correct answer but when I use nested for loop it only returns the values for E(:,3)=[1;10] . Can any one tell me where is the problem? Thanks in advance.
E=[0 0 1 1 2 2;1 96 10 87 37 60];
total =zeros(15,2)
for j=1:3
G=E(:,j);% G is [0;1],[0;96],[1;10] only first 3 columns of E
point=[];
for i=1:5
p(i,:)=i*G;
point=[p];
end
total = point(i,:);
end
Out put is
5 50
5 50
5 50
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Expexted answer is
[0 0 0 0 0 0 0 0 0 0 1 2 3 4 5;1 2 3 4 5 96 192 288 384 480]'
  1 commentaire
the cyclist
the cyclist le 14 Sep 2021
Modifié(e) : the cyclist le 14 Sep 2021
The first column of your expected answer has 15 rows, and the second column has 10 rows. That's not a valid MATLAB array.
So, I'm confused.
[0 0 0 0 0 0 0 0 0 0 1 2 3 4 5;1 2 3 4 5 96 192 288 384 480]'
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Connectez-vous pour commenter.

Réponse acceptée

Dave B
Dave B le 14 Sep 2021
Modifié(e) : Dave B le 14 Sep 2021
I think the question is really how you store your total values (you didn't store them in your snippet, so I'm not sure how you produced the result you expect.
I'm not sure that I would do this with a nested loop but I'd have to think about how to rearrange this into a reshape-type solution.
To fix your code and produce your expected answer:
E = [0 0 1 1 2 2;1 96 10 87 37 60];
total = zeros(15,2);
clc
for j=1:3
G=E(:,j);% G is [0;1],[0;96],[1;10] only first 3 columns of E
point=[]; %can initialize this (or really p as you know the exact size)
for i=1:5
p(i,:)=i*G;
point=[p]; %copying to point isn't adding much
total((j-1)*5+i,:)=point(i,:); %really this could just be = i*G and you could just get rid of point and p...
end
% alternatively, here could be total((j-1+1:5,:) = p;
end
total' % I'm transposing so we can see all the values:
ans = 2×15
0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 1 2 3 4 5 96 192 288 384 480 10 20 30 40 50
Here's a rewrite with my notes, but still keeping the loop approach:
E = [0 0 1 1 2 2;1 96 10 87 37 60];
total = zeros(15,2);
clc
for j=1:3
G=E(:,j);% G is [0;1],[0;96],[1;10] only first 3 columns of E
for i=1:5
total((j-1)*5+i,:)=i*G;
end
end
total'
ans = 2×15
0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 1 2 3 4 5 96 192 288 384 480 10 20 30 40 50
Here's a rewrite pulling out the inner loop:
E = [0 0 1 1 2 2;1 96 10 87 37 60];
total = zeros(15,2);
clc
for j=1:3
total( (j-1)*5 + (1:5),:) = ((1:5).*E(:,j))';
end
total'
ans = 2×15
0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 1 2 3 4 5 96 192 288 384 480 10 20 30 40 50
  3 commentaires
Dave B
Dave B le 14 Sep 2021
Modifié(e) : Dave B le 14 Sep 2021
No problem!
Actually I thought of a one-liner (still suspect there's a more efficient version). I always forget that repelem has a cool functionality with matrix arguments where you can repeat things in just the order that you're looking for:
E = [0 0 1 1 2 2;1 96 10 87 37 60];
repelem(E(:,1:3), 1, 5) .* repmat(1:5, 2, 3)
ans = 2×15
0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 1 2 3 4 5 96 192 288 384 480 10 20 30 40 50
lilly lord
lilly lord le 14 Sep 2021
Thank you .

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by