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

8 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2014-9-21
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 个评论
Giulia
Giulia 2014-9-22
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by