adding entities of 2 loops
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
summyia qamar
el 4 de En. de 2017
Comentada: summyia qamar
el 4 de En. de 2017
here is a code
[r1, c1]=size(Route)
for i=1:c1
E=0;
for j=1:Total_Operation
if Route{i}(j)~=0
E=E+Operations_Time{i}(j)*MakeToPart_Power(Route{i}(j));
end
end
Energy(i)=E
end
%setup_time
[r2,c2]=size(Route);
for k=1:c2
F=0;
for l=1:Total_Operation
if Route{k}(l)~=0
F=F+setup_Time{k}(l);
end
end
Setup(k)=F
end
after running, the results obtained are
Energy =
420 475 360 448 324 285 460 260 397 412 250 389
Setup =
7 9 12 10 7 7 8 7 7 9 6 8
I want to add each entity of both arrays for example
total=*420+7* *475+9* *360+12* etc..
and 2nd thing is there any better way to code them in one loop?
0 comentarios
Respuesta aceptada
Geoff Hayes
el 4 de En. de 2017
summyia - just combine the code. Both loops iterate over the columns of Route and since the Setup does not depend upon the Energy, you should be able to do
[r1, c1]=size(Route)
for i=1:c1
E=0;
F=0;
for j=1:Total_Operation
if Route{i}(j)~=0
E=E+Operations_Time{i}(j)*MakeToPart_Power(Route{i}(j));
F=F+setup_Time{k}(l);
end
end
Energy(i)=E;
Setup(i)=F;
end
Then the total would be
total = Energy + Setup;
Try the above and see what happens!
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!