Nested for loop for multipication
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
lilly lord
el 14 de Sept. de 2021
Comentada: lilly lord
el 14 de Sept. de 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 comentario
the cyclist
el 14 de Sept. de 2021
Editada: the cyclist
el 14 de Sept. de 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]'
Respuesta aceptada
Dave B
el 14 de Sept. de 2021
Editada: Dave B
el 14 de Sept. de 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:
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'
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'
3 comentarios
Dave B
el 14 de Sept. de 2021
Editada: Dave B
el 14 de Sept. de 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!