Got Questions? Get Answers.
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:
Binary Matrix Manipulation

Subject: Binary Matrix Manipulation

From: dhruv sharma

Date: 21 Jul, 2010 06:52:03

Message: 1 of 3

I want to find the first occurrence of 1 in each row of a binary matrix and convert all other elements to 0 AND convert all elements to the right of 1 to one. For example.
A = [0 0 0 1; 1 0 0 1; 0 0 0 0; 0 1 0 0]. Then I want two matrices: A1 = [ 0 0 0 1; 1 0 0 0; 0 0 0 0; 0 1 0 0] and A2 = [0 0 0 1; 1 1 1 1; 0 0 0 0; 0 1 1 1]. Thank you very much.

Subject: Binary Matrix Manipulation

From: Roger Stafford

Date: 21 Jul, 2010 18:01:16

Message: 2 of 3

"dhruv sharma" <stnfrd1980@yahoo.com> wrote in message <i265ej$aqv$1@fred.mathworks.com>...
> I want to find the first occurrence of 1 in each row of a binary matrix and convert all other elements to 0 AND convert all elements to the right of 1 to one. For example.
> A = [0 0 0 1; 1 0 0 1; 0 0 0 0; 0 1 0 0]. Then I want two matrices: A1 = [ 0 0 0 1; 1 0 0 0; 0 0 0 0; 0 1 0 0] and A2 = [0 0 0 1; 1 1 1 1; 0 0 0 0; 0 1 1 1]. Thank you very much.
- - - - - - - -
 A2 = +(cumsum(A,2)>0);
 A1 = diff([zeros(size(A,1),1),A2],1,2);

Roger Stafford

Subject: Binary Matrix Manipulation

From: dhruv sharma

Date: 21 Jul, 2010 19:59:05

Message: 3 of 3

Thanks a lot Roger. This works perfectly. -Dhruv

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i27clc$3dt$1@fred.mathworks.com>...
> "dhruv sharma" <stnfrd1980@yahoo.com> wrote in message <i265ej$aqv$1@fred.mathworks.com>...
> > I want to find the first occurrence of 1 in each row of a binary matrix and convert all other elements to 0 AND convert all elements to the right of 1 to one. For example.
> > A = [0 0 0 1; 1 0 0 1; 0 0 0 0; 0 1 0 0]. Then I want two matrices: A1 = [ 0 0 0 1; 1 0 0 0; 0 0 0 0; 0 1 0 0] and A2 = [0 0 0 1; 1 1 1 1; 0 0 0 0; 0 1 1 1]. Thank you very much.
> - - - - - - - -
> A2 = +(cumsum(A,2)>0);
> A1 = diff([zeros(size(A,1),1),A2],1,2);
>
> Roger Stafford

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