Efficient way to find the first value that exceeds limits in a 1column array

Hello,
I have a matrix with only one column of measures (eye coordinates on the screen on the x-axis). The code needs to find the first value that goes beyond a specific limit.
In other words, the matrix starts with values in the range 120:140 (which indicate the position of the eye is approximately on the center of the screen)Then, the values can be either (< 120) or (> 140) (indicating the eye moved towards the left or the right). I need to find the (i-row,1) position in which the values go beyond the limit of 120 or 140 (I cannot know in advance the direction of the eye).
________
i.e the raw data in my matrix could be something like that:
EyeRawData(:,1)= [130,135,125,136,131,...,128,121,116,90,76,65,65,54,65,70]
or
EyeRawData(:,1)= [130,135,125,136,131,...,139,142,156,170,190,191,189,187,190]
I need to count trough the entire rawdata 'how many rows it takes to get to the value 121 (or 142)'. And return EyeRawData (i,1)
___________
So, at the moment, I have written something like this:
l= length(EyeRawData);
for i=1:l
if (EyeRawData(i,l) > 140) || (EyeRawData(i,l) < 120)
position= i;
break
end
end
Can you please tell me whether this is a correct way of doing it or there are more efficient ways?
thank you very much
Giulia

 Akzeptierte Antwort

My approach would be to use the find function:
ERDI = find( (EyeRawData > 140) | (EyeRawData < 120) );
returning the indices of ‘EyeRawData’ that correspond to either condition.

4 Kommentare

To find the first occurrence, modify as
position = find( (EyeRawData > 140) | (EyeRawData < 120) , 1 );
Thank you Matt J. By the time I read through the question and experimented with it, I forgot about the ‘first value’ part.
Thank you very much Star Strider and Matt J. This is very useful and I can get loads of other important info with just one line.
Best
Giulia

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by