File Exchange

image thumbnail


version 1.7 (17.8 KB) by

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



View License

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

(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.

Comments/suggestions/error reports are welcome.
p10v5 - Update strongly recommended due to major bug in output sorting in v4.

Comments and Ratings (12)


Ist (view profile)

Thanks! Just what I needed.

Ele Demaria

Fantastic! Thank you for sharing it.

Oleg Komarov

Oleg Komarov (view profile)

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).

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);
I didn't check if I broke some functionality by adding this code, so please have a look at it yourself.
Nevertheless very helpful function.

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


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:
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)');

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

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

% Size of A
szA = size(A);

Zachary Danziger

>> 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 (view profile)


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, [])



Fixed major bug in the sorting of Final position that relied on regularity conditions not always verified


Per Herbert Gsenger's suggestion fixed bug in matching initial and final positions; minor change to distribution of OUT if multiple outputs; added 3D example


Per Zachary Danziger's suggestion reorganized check structure to avoid bug when concatenating results


Cast double on logical instead of single when treating NaNs and Infs


Per Xiaohu's suggestion fixed bug in output dimensions when A is row vector


Reorganized code and fixed bug when concatenating results

MATLAB Release
MATLAB 7.14 (R2012a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video