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?
댓글 수: 0
채택된 답변
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!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!