Okay so I want to find the maximum value of s found in the following loop:
for k=1:N;
x = sign(rand-0.5);
if x==1
s = s+1;
elseif x==-1
disp(s)
s=0;
end
end
Obviously it keeps resetting to 0 and then building back up again, but I want to be able to find the maximum value of s found throughout the loop, without displaying all values of s and searching for the maximum (I'll have to make N very big later!). Can anyone help please? Thank you!

 Akzeptierte Antwort

Torsten
Torsten am 12 Feb. 2018
max_s = -1;
for k=1:N;
x = sign(rand-0.5);
if x==1
s = s+1;
elseif x==-1
disp(s)
s=0;
end
max_s = max(max_s,s);
end
max_s

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by