Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Lengths of Continuous Line/Vector

Subject: Lengths of Continuous Line/Vector

From: max plank

Date: 6 Jan, 2013 18:18:37

Message: 1 of 5

Let us define vector y = [0 0 0 1 1 1 1 0 0 0 1 1 0]
I would like to extract the length of the continuous line
(i.e: ones = [4,2], zeros = [3,3,1])
I tried y(1:end-1)>y(2:end) and y(1:end-1)<y(2:end) but in case y start in ones or in case y(1) ~= y(end) it make me a lot of problem.

Is there a better way to extract those lengths?

Subject: Lengths of Continuous Line/Vector

From: Roger Stafford

Date: 6 Jan, 2013 21:40:08

Message: 2 of 5

max plank <avrialony@gmail.com> wrote in message <5f1a24c9-122c-4cf7-9ab4-d816839abc2c@googlegroups.com>...
> Let us define vector y = [0 0 0 1 1 1 1 0 0 0 1 1 0]
> I would like to extract the length of the continuous line
> (i.e: ones = [4,2], zeros = [3,3,1])
- - - - - - - - - -
 d = diff([0,find(diff(x)~=0),length(x)]);
 Zeros = d((x(1)>0)+1:2:end);
 Ones = d((x(1)<1)+1:2:end);

Note: You should avoid naming any variables 'ones' or 'zeros' (all lowercase) since those are names of matlab built-in functions.

Roger Stafford

Subject: Lengths of Continuous Line/Vector

From: Andrew

Date: 14 Jun, 2014 12:46:13

Message: 3 of 5

"Roger Stafford" wrote in message <kccqvo$etb$1@newscl01ah.mathworks.com>...
> max plank <avrialony@gmail.com> wrote in message <5f1a24c9-122c-4cf7-9ab4-d816839abc2c@googlegroups.com>...
> > Let us define vector y = [0 0 0 1 1 1 1 0 0 0 1 1 0]
> > I would like to extract the length of the continuous line
> > (i.e: ones = [4,2], zeros = [3,3,1])
> - - - - - - - - - -
> d = diff([0,find(diff(x)~=0),length(x)]);
> Zeros = d((x(1)>0)+1:2:end);
> Ones = d((x(1)<1)+1:2:end);
>
> Note: You should avoid naming any variables 'ones' or 'zeros' (all lowercase) since those are names of matlab built-in functions.
>
> Roger Stafford

Can this be applied to vectors that are not binary for example y = [0 0 0 1 1 2 2 2 3 4 4]?

Subject: Lengths of Continuous Line/Vector

From: dpb

Date: 14 Jun, 2014 14:30:18

Message: 4 of 5

On 6/14/2014 7:46 AM, Andrew wrote:
> "Roger Stafford" wrote in message
> <kccqvo$etb$1@newscl01ah.mathworks.com>...
>> max plank <avrialony@gmail.com> wrote in message
>> <5f1a24c9-122c-4cf7-9ab4-d816839abc2c@googlegroups.com>...
>> > Let us define vector y = [0 0 0 1 1 1 1 0 0 0 1 1 0]
>> > I would like to extract the length of the continuous line
>> > (i.e: ones = [4,2], zeros = [3,3,1])
>> - - - - - - - - - - d = diff([0,find(diff(x)~=0),length(x)]);
>> Zeros = d((x(1)>0)+1:2:end);
>> Ones = d((x(1)<1)+1:2:end);
...
> Can this be applied to vectors that are not binary for example y = [0 0
> 0 1 1 2 2 2 3 4 4]?

W/ some modifications, sure. The diff() operation will find the
breakpoints between sections; just that there will be one for every
level change and the levels themselves aren't known w/o further analysis
of the underlying data values...

--

Subject: Lengths of Continuous Line/Vector

From: Bruno Luong

Date: 15 Jun, 2014 07:06:08

Message: 5 of 5


> Can this be applied to vectors that are not binary for example y = [0 0 0 1 1 2 2 2 3 4 4]?

diff(find([true diff(y) true]))

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us