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.

 Accepted Answer

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 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!