From: dhruv sharma on
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.
From: Roger Stafford on
"dhruv sharma" <stnfrd1980(a)yahoo.com> wrote in message <i265ej$aqv$1(a)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
From: dhruv sharma on
Thanks a lot Roger. This works perfectly. -Dhruv

"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i27clc$3dt$1(a)fred.mathworks.com>...
> "dhruv sharma" <stnfrd1980(a)yahoo.com> wrote in message <i265ej$aqv$1(a)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