Compare the result of loop

조회 수: 4 (최근 30일)
Santhosh Chandrasekar
Santhosh Chandrasekar 2018년 2월 25일
답변: Shounak Shastri 2018년 3월 27일
I want to compare the utilityD for every iteration if the new utiltiyD is greater that old one old UtilityD then the old UtilityD with corresponding A1,D1,P will be deleted and new UtiltyD with the corresponding A1,D1,P values will replace the old one. Can someone help me to incorporate the above-mentioned requirement in the below-mentioned code?
A= rand(221);
resultD=zeros(221,4,10);
for i=1:10
A1= 0 + (1-0).*rand(221,1);
X11= A*A1;
P= zeros(221,1);
P(:)= X11(:)/max(X11);
D1= 0 + (1-0).*rand(221,1);
UtilityD=0;
for j=1:221
UtilityD= (((1-P(j))*(D(j)/(D(j)+A1(j)))* V)-C) +UtilityD;
end
resultD(:,1,i)=A1;
resultD(:,2,i)=D1;
resultD(:,3,i)=P;
resultD(1,4,i)=UtilityD;

답변 (1개)

Shounak Shastri
Shounak Shastri 2018년 3월 27일
Add a temp variable in your code and store the new UtilityD value in temp.
At the end you can just add a if statement as follows
if temp > UtilityD
UtilityD = temp;
end
This will replace the old UtilityD value with the new one stored in temp variable.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by