adding entities of 2 loops

조회 수: 1 (최근 30일)
summyia qamar
summyia qamar 2017년 1월 4일
댓글: summyia qamar 2017년 1월 4일
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