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