Finding the number of consecutive points in a given vector, using a condition

suppose i have a vector A=[0.2, 0.3, 4,5,6,7,0.2,0.4,0.5,0.3];
I have to find all those points which are less than 1, and occur consecutively more than 3 times.
ie. for the given vector A, i would get [0.2,0.4,0.5,0.3]
kindly help me write a general code for say, A being n x 1 vector, find all those points which are less than x, and occur consecutively more than y times.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 7월 30일
편집: Andrei Bobrov 2012년 7월 30일
with function regionprops from Image Processing Toolbox
idx = regionprops(A<1,'Area','PixelIdxList');
out = cellfun(@(x)A(x),{idx([idx.Area] > 3).PixelIdxList},'un',0);
other way
t = A<1;
z = [find([true,diff(t)~=0]) numel(A)+1];
idx = [z(1:end-1);z(2:end)-1];
i1 = idx(:,t(idx(1,:)));
i2 = i1(:,(diff(i1) + 1) > 3);
out = arrayfun(@(x)A(i2(1,x):i2(2,x)),1:size(i2,2),'un',0);
third way
t = A<1;
t1 = [~t(1) t ~t(end)];
idx = [strfind(t1,[0 1]);strfind(t1,[1 0])-1];
i1 = idx(:,diff(idx) + 1 > 3);
out = arrayfun(@(x)A(i2(1,x):i2(2,x)),1:size(i2,2),'un',0);

댓글 수: 2

fantastic,thanks a lot!
just a comment: the first one doesn't work for say A=[0.2, 0.3, 4,5,6,7,0.2,0.4,0.5,0.3,3,2,1,0.1,0.1,0.1,0.1];
Hi Vishesh! I was corrected this is answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by