find the row number of values greater than...

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

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-8-15
编辑:Andrei Bobrov 2012-8-15
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 个评论
Richard
Richard 2012-8-15
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 2012-8-15
编辑:Andrei Bobrov 2012-8-15
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;

请先登录,再进行评论。

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-8-15
编辑:Azzi Abdelmalek 2012-8-15
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)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by