Compare the result of loop

3 次查看(过去 30 天)
Santhosh Chandrasekar
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