find the row number of values greater than...

14 views (last 30 days)
I would like to find in which column a desired number is first located. From the example below:
x = [0,1,3,2,5,22,24,1,2,6,0.3,.5,30,43,45,32,22,56];
Here, I would like to know where the first number greater than 20 is located. However, if the number of successive values >20 is less than 3 I would like to move onto the next set of values. So the outcome from x (above) should be 13 i.e. the first value greater than 20 which is followed by successive (more than 3) values greater then 20 is in column 13.
I realise that I can find the first value by:
a = find(x>20,1,'first');
but I dont know how to complete what is described above.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 15 Aug 2012
Edited: Andrei Bobrov on 15 Aug 2012
One way:
y = x > 20;
t = [true, diff(y) ~= 0];
k = find(t);
k = [k;k(2:end)-1,numel(t)];
idx = k(1,all(y(k)) & diff(k)+1 > 3);
second way (for vector-row):
y = x > 20;
k = [strfind([~y(1),y],[0 1]);strfind([y,~y(end)],[1 0])];
idx = k(1,diff(k) + 1 > 3);
other way ( Image Processing Toolbox ):
y = x > 20;
s = regionprops(y,'Area','PixelIdxList');
idxs = s([s.Area] > 3).PixelIdxList;
idx = idxs(1);
  2 Comments
Richard
Richard on 15 Aug 2012
The second method you mentioned works great. After finding that location how would it then be possible to remove the first values greater than 20 i.e. change them to nans? So, from this example x(6:7) to nan so that the peaks (if plotted) would only show for the second set of values.
Andrei Bobrov
Andrei Bobrov on 15 Aug 2012
Edited: Andrei Bobrov on 15 Aug 2012
for your case:
y = x > 20;
k = [strfind([~y(1),y],[0 1]);strfind([y,~y(end)],[1 0])];
idx = k(1,diff(k) + 1 > 3);
idxx = find(y);
x(idxx(idxx < idx)) = nan;

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Aug 2012
Edited: Azzi Abdelmalek on 15 Aug 2012
x = [0,1,3,2,5,22,24,1,2,6,0.3,.5,30,43,45,32,22,56];
a = find(x>20);l=1;
for k=1:length(a)-2
if a(k+2)==a(k+1)+1 & a(k+1)==a(k)+1
ind(l)=a(k);l=l+1;
end
end
result=min(ind)

Categories

Find more on Images in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!