필터 지우기
필터 지우기

How to use a loop to get the final result stated in problem

조회 수: 1 (최근 30일)
Khadija Khan
Khadija Khan 2019년 3월 20일
댓글: Kevin Phung 2019년 3월 21일
Hello everyone,
I am trying to get output as 2*4 matrix. You can find my simple code as:
t=2;
kr=2;
s=[0;0];
R=[2 3; 3 4];
D=[4 5; 5 6];
s1=zeros(kr,t);
for i=1:kr
for j=1:t
s1(i,j)=max(s(i)-R(i,j),0)+D(i,j);
s(i)=s1(i,j);
end
end
s2=zeros(kr,t);
for i=1:kr
for j=1:t
s2(i,j)=max(s(i)-R(i,j),0)+D(i,j);
s(i)=s2(i,j)
end
end
S=[s1 s2]; % final output
I would like to use a single loop rather than repeating the loop for and to get this final output of S (2*4).. It's okay for only s1 and s2 but I have . Any help is appreciated.
Regards

채택된 답변

Kevin Phung
Kevin Phung 2019년 3월 20일
t=2;
kr=2;
s=[0;0];
R=[2 3; 3 4];
D=[4 5; 5 6];
s1=zeros(kr,t);
S = [];
for n = 1:100
for i=1:kr
for j=1:t
s1(i,j)=max(s(i)-R(i,j),0)+D(i,j);
s(i)=s1(i,j);
end
end
S = [S s1];
end
does this do what you want?
  댓글 수: 2
Khadija Khan
Khadija Khan 2019년 3월 21일
@Kevin Phung Thank you so much. It's working fine.
Kevin Phung
Kevin Phung 2019년 3월 21일
happy to help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by