using if to find a value in one variable, but then give you the value of a second variable

Hi all,
Ok so I have river runoff data for around 100 days, and I would lke to find the time of maximum runoff on each day. I reshaped the data (Q) into an x,y matrix (hours,days), but so far have had no luck getting the code to work. I tried it first with made up data and the code below worked fine:
for x=1:24;
for y=1:4;
if Q(x,:)==max(y)
Peak=T(x,:);
end
end
end
The variable T is matrix of the time with 1:24 repeated for each day. Peak is the variable of time of maximum runoff.Trouble is I can't get this to work with my real data, I have already taken out the NaNs and replaced them with 1. There is no error it just runs through without making Peak. I found the maximum for each day sepeately and it works fine for days without NaNs. Any ideas??
I would be greatful for any suggestions, Thanks, Cat

 Accepted Answer

[val,idx] = max(Q)
This is the row-wise maximum, i.e. along the rows for each column. idx says on which row the max is found. Since each day is a different column, the idx tells you which hour.

1 Comment

Thanks, that's a much more concise way of doing it...I haven't learnt these clever Matlab ways yet!!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!