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:
vector with elements 0 and 1

Subject: vector with elements 0 and 1

From: 1986

Date: 5 Aug, 2010 22:52:04

Message: 1 of 4

Hi everyone,
I'm new here and a begginer at matlab.
I have vector with elements 0 and 1. Vector length is 300.
I need to find the longest array of 1 and its location in vector.
Does anyone have any suggestions?
Thank you in advance,
Milana

Subject: vector with elements 0 and 1

From: Matt J

Date: 5 Aug, 2010 22:59:06

Message: 2 of 4

If you have the Image Processing Toolbox, use bwconncomp() and regionprops().

Subject: vector with elements 0 and 1

From: Roger Stafford

Date: 6 Aug, 2010 00:19:05

Message: 3 of 4

"1986 " <milanaplecas@gmail.com> wrote in message <i3ffak$8qm$1@fred.mathworks.com>...
> Hi everyone,
> I'm new here and a begginer at matlab.
> I have vector with elements 0 and 1. Vector length is 300.
> I need to find the longest array of 1 and its location in vector.
> Does anyone have any suggestions?
> Thank you in advance,
> Milana
- - - - - - - - -
  If you don't have the image processing toolbox, you can do this. Let the vector of 1's and 0's be a row vector.

 t = diff([0,v,0]);
 p1 = find(t==1);
 p2 = find(t==-1);
 [m,p] = max(f2-f1);
 b = p1(p);
 e = p2(p)-1;

m is the length of the longest sequence of ones, b is the index of its first element and e is the index of its last element.

Roger Stafford

Subject: vector with elements 0 and 1

From: 1986

Date: 6 Aug, 2010 11:01:05

Message: 4 of 4

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i3fkdp$mcr$1@fred.mathworks.com>...
> "1986 " <milanaplecas@gmail.com> wrote in message <i3ffak$8qm$1@fred.mathworks.com>...
> > Hi everyone,
> > I'm new here and a begginer at matlab.
> > I have vector with elements 0 and 1. Vector length is 300.
> > I need to find the longest array of 1 and its location in vector.
> > Does anyone have any suggestions?
> > Thank you in advance,
> > Milana
> - - - - - - - - -
> If you don't have the image processing toolbox, you can do this. Let the vector of 1's and 0's be a row vector.
>
> t = diff([0,v,0]);
> p1 = find(t==1);
> p2 = find(t==-1);
> [m,p] = max(f2-f1);
> b = p1(p);
> e = p2(p)-1;
>
> m is the length of the longest sequence of ones, b is the index of its first element and e is the index of its last element.
>
> Roger Stafford

Thank you Roger,
It is exactly what I'm looking for.
Milana

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