Compare the result of loop

5 visualizaciones (últimos 30 días)
Santhosh Chandrasekar
Santhosh Chandrasekar el 25 de Feb. de 2018
Respondida: Shounak Shastri el 27 de Mzo. de 2018
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;

Respuestas (1)

Shounak Shastri
Shounak Shastri el 27 de Mzo. de 2018
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.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by