Series of consecutive numbers (different lengths)
Asked Answered
L

3

6

I would appreciate if someone showed me an easy way to do this. Let's say I have a vector in MATLAB like

d = [3 2 4 2 2 2 3 5 1 1 2 1 2 2 2 2 2 9 2]

I want to find the series of consecutive number "twos" and the lengths of those series.

Number twos can easily be found by x=find(d==2). But what I want is to get a vector which contains the lengths of all series of consecutive number twos, which means that my result in this case would be a vector like this:

[1 3 1 5 1].

Anyone who could help me?

Lymph answered 21/10, 2011 at 13:49 Comment(1)
related question: MATLAB: finding islands of zeros in a sequenceCzernowitz
A
9

This seems to work:

q = diff([0 d 0] == 2);
v = find(q == -1) - find(q == 1);

gives

v =

   1   3   1   5   1

for me

Arabic answered 21/10, 2011 at 14:12 Comment(4)
what if the v is not a vector and is dynamic? if number of success is 5 consecutive then win?can you help @MAXOutlook
@Outlook - sorry, I'm not sure I follow your question. The only way v will not be a vector is if there is only one run of 2's, in which case it will be a scalar, or if there are no 2's present, in which case it will be []. I don't know what you mean by 'dynamic' here - could be a bit more explicit?Arabic
Yes here you have a vector @max, my question is; lets say you have a function which will be repeated 10 times. during each run v will be 1 or 0.what i mean by dynamic is do not wait 10 times save value of v then find n consecutive 1s. i mean the test will be done during each run of the function. Hope i am clear and thanksOutlook
This answer has been explained in detail here.Leges
M
6

This is called run length encoding. There is a good m-file available for it at http://www.mathworks.com/matlabcentral/fileexchange/4955-rle-deencoding . This method is generally faster than the previously posted diff/find way.

tic
d_rle = rle(d==2);
d_rle{2}(d_rle{1}==1);
toc

Elapsed time is 0.002632 seconds.

tic
q = [0 diff([0 d 0] == 2)];
find(q == -1) - find(q == 1);
toc

Elapsed time is 0.003061 seconds.

Muenster answered 21/10, 2011 at 18:52 Comment(2)
Yep - rle() only has one call to find(), and find() is pretty slowArabic
@Arabic neat...I've never opened it up to find out why. Thanks!Muenster
A
-1

What if we want the indices of the original matrix where the consecutive values are located? Further, what if we want a matrix of the same size as the original matrix, where the number of consecutive values are stored in the indices of the consecutive values? For example:

  original_matrix = [1 1 1;2 2 3; 1 2 3];

  output_matrix = [3 3 3;2 2 0;0 0 0];

This problem has relevance for meteorological data quality control. For example, if I have a matrix of temperature data from a number of sensors, and I want to know what days had constant consecutive values, and how many days were constant, so I can then flag the data as possibly faulty.

temperature matrix is number of days x number of stations and I want an output matrix that is also number of days x number of stations, where the consecutive values are flagged as described above.

Anchusin answered 9/11, 2014 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.