MATLAB: Increase speed of for loop used to evaluate data from a large array, 70000×1

for looplarge arraymaximumspeed

Hi,
I have a large array H = 70000×1 where I need to calculate the quantity, EH = max( H(t) – min(H(t') ), where the maximum is evaluated over all t and t'>=t. My code is:
EH = zeros(length(H),1);
for i = 1:length(H)
EH(i)= H(i)-min(H(i:length(H)));
end
EH = max(EH);
The calculation is slow, around 5-6 seconds. How do I increase the speed off this calculation?
Best regards, Kenneth

Best Answer

I don't know if it's any faster ( edit: yep, much faster 8 ms vs 4.4 s) but your whole code is equivalent to:
EH = max(H - cummin(H, 'reverse'));
side note:
H(i:length(H))
can be written as
H(i:end)
Related Question