Code covered by the BSD License  

Highlights from
FINDSEQ

5.0
5.0 | 6 ratings Rate this file 28 Downloads (last 30 days) File Size: 17.8 KB File ID: #28113 Version: 1.7
image thumbnail

FINDSEQ

by

Oleg Komarov (view profile)

 

02 Jul 2010 (Updated )

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

| Watch this File

File Information
Description

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.

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

Acknowledgements

Rude: A Pedestrian Run Length Decoder Encoder and Getchunks inspired this file.

This file inspired Clipping Indicator.

MATLAB release MATLAB 7.14 (R2012a)
Other requirements Tested on R14SP3 (7.1) and on R2013a. In-between compatibility is assumed.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (11)
04 Jun 2015 Ele Demaria

Fantastic! Thank you for sharing it.

Comment only
21 Mar 2012 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).

Comment only
19 Mar 2012 Herbert Gsenger

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.
Nevertheless very helpful function.

19 Mar 2012 Jonathan Sullivan

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

13 Jul 2011 Stefan

Stefan (view profile)

 
16 Nov 2010 Brahim HAMADICHAREF

Useful function. Thanks

28 Aug 2010 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);

Comment only
27 Aug 2010 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.

Comment only
12 Jul 2010 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.

Comment only
10 Jul 2010 Xiaohu

Xiaohu (view profile)

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

Updates
07 Jul 2010 1.1

Reorganized code and fixed bug when concatenating results

12 Jul 2010 1.2

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

27 Aug 2010 1.3

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

30 Aug 2010 1.4

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

22 Mar 2012 1.6

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

11 Nov 2013 1.7

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

Contact us