Compare the result of loop

1 回表示 (過去 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 Center および File ExchangeEarth and Planetary Science についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by