Nested for loop for multipication

조회 수: 2 (최근 30일)
lilly lord
lilly lord 2021년 9월 14일
댓글: lilly lord 2021년 9월 14일
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
the cyclist
the cyclist 2021년 9월 14일
편집: the cyclist 2021년 9월 14일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Dave B
Dave B 2021년 9월 14일
편집: Dave B 2021년 9월 14일
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
Dave B
Dave B 2021년 9월 14일
편집: Dave B 2021년 9월 14일
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 2021년 9월 14일
Thank you .

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by