Truncating vector to longest continuous string of numbers

I have a number of 10000x1 arrays with some bad data and only want to keep the longest continuous part of the array. I have spent quite a while searching but cannot find an answer.
So what I am looking for is a way for MATLAB to search each vector, find the longest continuous set of numbers above a certain value and discard the rest. This should be simple but I cannot found and easy solution.
eg m = [1,2,3,4,5,6,-5,-5,-5,1,2,3,4,5,6,7,-5,-5,-5,-5,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; would return m=[ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ]
Thanks
EDIT the array is not necessarily continuous numbers.

1 commentaire

<Previous?> has several answers plus there's a submittal on File Exchange on finding runs--I forget the exact name but a search should uncover it...

Connectez-vous pour commenter.

Réponses (2)

Andrei Bobrov
Andrei Bobrov le 15 Juin 2017
Modifié(e) : Andrei Bobrov le 15 Juin 2017
m=[ 1 2 3 4 5 6 -5 -5 -5 1 2 3 4 5 6 7 -5 -5 -5 -5 1 2 3 4];
z = diff(m) == 1;
z = [z(1);z(:)];
z(diff(z)==1) = 1;
a = zeros(numel(z),1);
a(diff([0;z]) == 1) = 1;
a = cumsum(a,1).*z;
n = accumarray(a+1,1);
[~,ii] = max(n(2:end));
out = m(a == ii);
or
z = diff(m) == 1;
z = [z(1),z(:)'];
z(strfind(z,[0 1])) = 1;
a = regionprops(z,'Area','PixelIdxList');
out = m(a([a.Area] == max([a.Area])).PixelIdxList);

1 commentaire

Thanks very much for your help but it is the same as the comment below. The numbers in my array are not consecutive hence if my array is m = [1,2,3,4,5,6,-5,-5,-5,1,2,3,4,5,6,7,-5,-5,-5,-5,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; your code pulls out 1 2 3 4 5 6 7 not the string of ones. thanks

Connectez-vous pour commenter.

Stephen23
Stephen23 le 15 Juin 2017
Modifié(e) : Stephen23 le 15 Juin 2017
>> m = [1,2,3,4,5,6,-5,-5,-5,1,2,3,4,5,6,7,-5,-5,-5,-5,1,2,3,4];
>> d = diff([false,diff(m)==1,false]);
>> vb = find(d>0);
>> ve = find(d<0);
>> [~,idx] = max(ve-vb) % which sequence is the longest
idx = 2
>> m(vb(idx):ve(idx)) % get that sequence
ans =
1 2 3 4 5 6 7

2 commentaires

Thanks but I missed something when writing my question. The numbers in the array are not consecutive hence if my array is m = [1,2,3,4,5,6,-5,-5,-5,1,2,3,4,5,6,7,-5,-5,-5,-5,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; your code pulls out 1 2 3 4 5 6 7 not the string of ones. thanks though it gives me something to work on.
Stephen23
Stephen23 le 15 Juin 2017
Modifié(e) : Stephen23 le 15 Juin 2017
@nickname1: so what you want is the longest sequence of positive values? Like this?:
>> m = [1,2,3,4,5,6,-5,-5,-5,1,2,3,4,5,6,7,-5,-5,-5,-5,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
>> d = diff([false,m>0,false]);
>> vb = find(d>0);
>> ve = find(d<0)-1;
>> [~,idx] = max(ve-vb) % which sequence is the longest
idx = 3
>> m(vb(idx):ve(idx)) % get that sequence
ans =
1 2 3 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Connectez-vous pour commenter.

Catégories

Question posée :

le 15 Juin 2017

Modifié(e) :

le 15 Juin 2017

Community Treasure Hunt

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

Start Hunting!

Translated by