adding entities of 2 loops

1 次查看(过去 30 天)
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?

采纳的回答

Geoff Hayes
Geoff Hayes 2017-1-4
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!
  1 个评论
summyia qamar
summyia qamar 2017-1-4
thankyou so much..just before checking the answer, I did the same. :) your positive vibrations reached to me already

请先登录,再进行评论。

更多回答(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