find the row number of values greater than...
12 ビュー (過去 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.
0 件のコメント
採用された回答
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 件のコメント
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
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)
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!