find the row number of values greater than...

조회 수: 12 (최근 30일)
Richard
Richard 2012년 8월 15일
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