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:
Locating matrix column that meets certain criteria

Subject: Locating matrix column that meets certain criteria

From: Ulrik Nash

Date: 17 Sep, 2010 16:08:05

Message: 1 of 5

Hi Everyone,

Suppose I have a matrix A:

A = [0 0 1 0 1 1;0 0 1 1 1 1]

Now I wish to find that column, which not only contains all ones, but also contains no columns on its right that hold zeros. So in this case, the solution would be 5.

Any suggestions how I can do this neatly?

Regards,

Ulrik.

Subject: Locating matrix column that meets certain criteria

From: Ulrik Nash

Date: 17 Sep, 2010 16:15:22

Message: 2 of 5

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i703p5$p47$1@fred.mathworks.com>...
> Hi Everyone,
>
> Suppose I have a matrix A:
>
> A = [0 0 1 0 1 1;0 0 1 1 1 1]
>
> Now I wish to find that column, which not only contains all ones, but also contains no columns on its right that hold zeros. So in this case, the solution would be 5.
>
> Any suggestions how I can do this neatly?
>
> Regards,
>
> Ulrik.



Should read, "but also HAS no columns on its right that hold zeros".

Subject: Locating matrix column that meets certain criteria

From: Sean

Date: 17 Sep, 2010 16:26:19

Message: 3 of 5

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i7046q$n9t$1@fred.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <i703p5$p47$1@fred.mathworks.com>...
> > Hi Everyone,
> >
> > Suppose I have a matrix A:
> >
> > A = [0 0 1 0 1 1;0 0 1 1 1 1]
> >
> > Now I wish to find that column, which not only contains all ones, but also contains no columns on its right that hold zeros. So in this case, the solution would be 5.
> >
> > Any suggestions how I can do this neatly?
> >
> > Regards,
> >
> > Ulrik.
>
>
>
> Should read, "but also HAS no columns on its right that hold zeros".

One way:
idx = max(find(any(~A,1)))+1

Subject: Locating matrix column that meets certain criteria

From: Frank

Date: 17 Sep, 2010 16:26:39

Message: 4 of 5

For the matrix you give, this works....

B = all(A);

find(B(1:end-1) & B(2:end))

Subject: Locating matrix column that meets certain criteria

From: Steven_Lord

Date: 17 Sep, 2010 17:19:37

Message: 5 of 5



"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
news:i703p5$p47$1@fred.mathworks.com...
> Hi Everyone,
>
> Suppose I have a matrix A:
>
> A = [0 0 1 0 1 1;0 0 1 1 1 1]
>
> Now I wish to find that column, which not only contains all ones, but also
> contains no columns on its right that hold zeros. So in this case, the
> solution would be 5.
>
> Any suggestions how I can do this neatly?

Find the last column with a 0; the value you want is one past that.

c = find(~all(A, 1), 1, 'last')+1

You should test this after computing it to ensure c is neither empty (if
there is no column containing a zero, in which case I guess you'd want
column 1) nor size(A, 2)+1 (in which case the last column of A contained a
zero; you haven't specified what to return in that case.)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for 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