Nested for loop for multipication

3 views (last 30 days)
lilly lord
lilly lord on 14 Sep 2021
Commented: lilly lord on 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 Comment
the cyclist
the cyclist on 14 Sep 2021
Edited: the cyclist on 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.

Sign in to comment.

Accepted Answer

Dave B
Dave B on 14 Sep 2021
Edited: Dave B on 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 Comments
Dave B
Dave B on 14 Sep 2021
Edited: Dave B on 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

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!