File Exchange

## FINDSEQ

version 1.7 (17.8 KB) by

Find sequences of repeated (adjacent/consecutive) numeric values. NaNs and Infs are supported.

Updated

Find sequences of repeated (adjacent/consecutive) numeric values. Finds sequences of NaNs and Infs too.

SYNTAXES:
(1) FINDSEQ(A) Find sequences of repeated numeric values in A along the first non-singleton dimension. A shuld be a numeric nD matrix.
(2) FINDSEQ(A,DIM) Look for sequences along the dimension specified by the positive integer scalar DIM.

OUT = findseq(...)
OUT is a "m by 4" numeric matrix where m is the number of sequences found.
Each sequence has 4 columns where:
- 1st col.: the value being repeated
- 2nd col.: the position of the first value of the sequence
- 3rd col.: the position of the last value of the sequence
- 4th col.: the length of the sequence

[VALUES, INPOS, FIPOS, LEN] = findseq(...)
Get OUT as separate outputs.

For details see help
For examples see the attached image.

p10v5 - Update strongly recommended due to major bug in output sorting in v4.

Ist

### Ist (view profile)

Thanks! Just what I needed.

Ele Demaria

### Ele Demaria (view profile)

Fantastic! Thank you for sharing it.

Oleg Komarov

### Oleg Komarov (view profile)

@Herbert,
thanks for pointing that out, I will try to check if this fix can be generalized to N-D arrays, otherwise I will rewrite the engine to basically allow for a FIND function that works in the direction specified by DIM other than along the rows only (DIM=1).

Herbert Gsenger

### Herbert Gsenger (view profile)

Hello Oleg,
please have a look at the following bug:
a = [1, 1, 1; 2, 2, 3; 3, 4, 4];
findseq(a, 2)
ans =

1 1 5 2
2 2 7 3
4 6 9 2
The correct result would be:
ans =

1 1 7 3
2 2 5 2
4 6 9 2

A quick fix was to add the following code at line 144 (mainengine).
if dim == 2
[rows, ~] = ind2sub(size(IDX), FiPos);
FiPos = FiPos(rows);
end
I didn't check if I broke some functionality by adding this code, so please have a look at it yourself.

Jonathan Sullivan

### Jonathan Sullivan (view profile)

Very nifty function. It is very efficient too. Well done!

Stefan

### Stefan (view profile)

Useful function. Thanks

Oleg Komarov

### Oleg Komarov (view profile)

Thanks Zachary,
you pointed out a problem in the check structure. I fixed it and the new release will be avaible hopefully on monday the 30th of aug.
In the meantime lines 67-87 should be substituted with:
% DIM
szA = size(A);
if nargin == 1 || isempty(dim)
% First non singleton dimension
dim = find(szA ~= 1,1,'first');
elseif ~(isnumeric(dim) && dim > 0 && rem(dim,1) == 0) || dim > numel(szA)
error('findseq:fmtDim', 'DIM should be a scalar positive integer <= ndims(A)');
end

% Less than two elements along DIM
if szA(dim) == 1
varargout{1} = [];
return
end

% ISVECTOR
if nnz(szA ~= 1) == 1
A = A(:);
dim = 1;
end

% Size of A
szA = size(A);

Zachary Danziger

### Zachary Danziger (view profile)

>> findseq([NaN NaN 0 NaN NaN NaN NaN NaN])
ans =
NaN 1 2 2
NaN 4 8 5

>> findseq([NaN NaN 0 NaN NaN NaN NaN NaN],1)
ans =
[]

>> findseq([NaN NaN 0 NaN NaN NaN NaN NaN],2)
ans =
NaN 1 1 4 2 8 2 5

Cases 1 and 2 make sense to me, but I don't understand the output of the 3rd case. Can you explain? Thanks.

Oleg Komarov

### Oleg Komarov (view profile)

I fixed the bug pointed out by Xiaohu and uploaded the new version which hopefully will be available later today or tomorrow.

Xiaohu

Xiaohu

### Xiaohu (view profile)

A small bug when output results for 1xn matrix:
findseq([1 1 2 3 3])
ans =
1 3 1 4 2 5 2 2
which should be reshaped by reshape(ans, 2, [])