MATLAB Answers

Nested for loop for multipication

3 ビュー (過去 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 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 件のコメント
lilly lord
lilly lord 2021 年 9 月 14 日
Thank you .

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by