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:
matrix manipulation

Subject: matrix manipulation

From: dhruv sharma

Date: 8 May, 2009 11:32:01

Message: 1 of 7

Hello:

I would really appreciate help for doing the following manipulation. I have a matrix A. It has non-zero numbers in each row followed by 0s.
A = [0.1, 0.2 ,0;
       0.2, 0, 0;
      0.3, 0.4, 0]

I want to convert this matrix to a another matrix B, such that each element before the first occurrence of 0 is subtracted by 1. So B looks like:
A = [0.1,0.8,0;
       0.8,0,0;
      0.3,0.7,0]

Thanks a lot for your help. Dhruv.

Subject: matrix manipulation

From: Jean-Christophe

Date: 8 May, 2009 11:50:03

Message: 2 of 7

On May 8, 12:32 pm, "dhruv sharma" <stnfrd1...@yahoo.com> wrote:

> I would really appreciate help for doing the following manipulation. I ha=
ve a matrix A. It has non-zero numbers in each row followed by 0s.
> A = [0.1, 0.2 ,0;
>        0.2, 0, 0;
>       0.3, 0.4, 0]
>
> I want to convert this matrix to a another matrix B, such that each eleme=
nt before the first occurrence of 0 is subtracted by 1. So B looks like:
> A = [0.1,0.8,0;
>        0.8,0,0;
>       0.3,0.7,0]

Since the element to be substraced from 1 is not always at
the same position in the matrix, and since you need to check
that the following element has a value of zero,
I think that you will have to use a FOR loop.

Subject: matrix manipulation

From: us

Date: 8 May, 2009 11:55:03

Message: 3 of 7

"dhruv sharma" <stnfrd1980@yahoo.com> wrote in message <gu157h$stq$1@fred.mathworks.com>...
> Hello:
>
> I would really appreciate help for doing the following manipulation. I have a matrix A. It has non-zero numbers in each row followed by 0s.
> A = [0.1, 0.2 ,0;
> 0.2, 0, 0;
> 0.3, 0.4, 0]
>
> I want to convert this matrix to a another matrix B, such that each element before the first occurrence of 0 is subtracted by 1. So B looks like:
> A = [0.1,0.8,0;
> 0.8,0,0;
> 0.3,0.7,0]

one of the many solutions

% the data
     m=[
          0.1 0.2 0
          0.2 0 0
          0.3 0.4 0
     ];
% the engine
     [r,c]=find(m==0);
     rc=sortrows([r,c]);
     ix=[true;diff(rc(:,1))~=0];
     lx=sub2ind(size(m),rc(ix,1),rc(ix,2)-1);
     lx=lx(lx>0);
     m(lx)=1-m(lx);
% the result
     disp(m);
%{
          0.1 0.8 0
          0.8 0 0
          0.3 0.6 0
%}

us

Subject: matrix manipulation

From: Jos

Date: 8 May, 2009 12:58:15

Message: 4 of 7

"dhruv sharma" <stnfrd1980@yahoo.com> wrote in message <gu157h$stq$1@fred.mathworks.com>...
> Hello:
>
> I would really appreciate help for doing the following manipulation. I have a matrix A. It has non-zero numbers in each row followed by 0s.
> A = [0.1, 0.2 ,0;
> 0.2, 0, 0;
> 0.3, 0.4, 0]
>
> I want to convert this matrix to a another matrix B, such that each element before the first occurrence of 0 is subtracted by 1. So B looks like:
> A = [0.1,0.8,0;
> 0.8,0,0;
> 0.3,0.7,0]
>
> Thanks a lot for your help. Dhruv.

0.7 ??

and what if there is no first zero?

Here is something that comes close:

q = fliplr(cumsum(fliplr(~~A),2)) == 1 & A~=0
A(q) = 1 - A(q)

which was inspired by this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/250469

Jos

Subject: matrix manipulation

From: dhruvnsharma@gmail.com

Date: 8 May, 2009 17:54:34

Message: 5 of 7

On May 8, 5:58 am, "Jos " <#10...@fileexchange.com> wrote:
> "dhruv sharma" <stnfrd1...@yahoo.com> wrote in message <gu157h$st...@fred=
.mathworks.com>...
> > Hello:
>
> > I would really appreciate help for doing the following manipulation. I =
have a matrix A. It has non-zero numbers in each row followed by 0s.
> > A = [0.1, 0.2 ,0;
> >        0.2, 0, 0;
> >       0.3, 0.4, 0]
>
> > I want to convert this matrix to a another matrix B, such that each ele=
ment before the first occurrence of 0 is subtracted by 1. So B looks like:
> > A = [0.1,0.8,0;
> >        0.8,0,0;
> >       0.3,0.7,0]
>
> > Thanks a lot for your help. Dhruv.
>
> 0.7 ??
>
> and what if there is no first zero?
>
> Here is something that comes close:
>
> q = fliplr(cumsum(fliplr(~~A),2)) == 1 & A~=0
> A(q) = 1 - A(q)
>
> which was inspired by this thread:http://www.mathworks.com/matlabcentral/=
newsreader/view_thread/250469
>
> Jos

Thanks a lot for your help. Both solutions work. But my problem has
now become 3D. So if
A(:,:,1) = [0.1, 0.2, 0;
            0.2, 0, 0;
            0.6, 0.4, 0]

A(:,:,2) = [0.2, 0.7, 0;
            0.3, 0.8, 0;
            0.1, 0, 0]
The changed matrix will be:
B(:,:,1) = [0.1, 0.8, 0;
            0.8, 0, 0;
            0.4, 0.6, 0]
B(:,:,2) = [0.2,0.3,0;
            0.3, 0.2, 0;
            0.9, 0, 0]

There is always a 0 at the end of a row.

Subject: matrix manipulation

From: Matt

Date: 8 May, 2009 20:14:02

Message: 6 of 7

dhruvnsharma@gmail.com wrote in message <739c8cb6-4e22-4e27-b9e4-5fed7639ce29@n7g2000prc.googlegroups.com>...

> Thanks a lot for your help. Both solutions work. But my problem has
> now become 3D. So if
> A(:,:,1) = [0.1, 0.2, 0;
> 0.2, 0, 0;
> 0.6, 0.4, 0]
>
> A(:,:,2) = [0.2, 0.7, 0;
> 0.3, 0.8, 0;
> 0.1, 0, 0]
> The changed matrix will be:
> B(:,:,1) = [0.1, 0.8, 0;
> 0.8, 0, 0;
> 0.4, 0.6, 0]
> B(:,:,2) = [0.2,0.3,0;
> 0.3, 0.2, 0;
> 0.9, 0, 0]
>

I think you have a typo in element B(3,1,1). I think you want B(3,1,1)=A(3,1,1)=0.6

If this is so, then I propose the following

K=[-1 1];
Mask=convn(logical(A),K,'same')>0;

B=A;
B(Mask)=1-B(Mask),

Subject: matrix manipulation

From: dhruv sharma

Date: 8 May, 2009 20:49:02

Message: 7 of 7

Matt you are right, I had a typo. Great! Matt's solution is working. Thanks for all your comments and help - this has saved me several hours of thinking how to do this. Dhruv.

"Matt " <xys@whatever.com> wrote in message <gu23qa$9q7$1@fred.mathworks.com>...
> dhruvnsharma@gmail.com wrote in message <739c8cb6-4e22-4e27-b9e4-5fed7639ce29@n7g2000prc.googlegroups.com>...
>
> > Thanks a lot for your help. Both solutions work. But my problem has
> > now become 3D. So if
> > A(:,:,1) = [0.1, 0.2, 0;
> > 0.2, 0, 0;
> > 0.6, 0.4, 0]
> >
> > A(:,:,2) = [0.2, 0.7, 0;
> > 0.3, 0.8, 0;
> > 0.1, 0, 0]
> > The changed matrix will be:
> > B(:,:,1) = [0.1, 0.8, 0;
> > 0.8, 0, 0;
> > 0.4, 0.6, 0]
> > B(:,:,2) = [0.2,0.3,0;
> > 0.3, 0.2, 0;
> > 0.9, 0, 0]
> >
>
> I think you have a typo in element B(3,1,1). I think you want B(3,1,1)=A(3,1,1)=0.6
>
> If this is so, then I propose the following
>
> K=[-1 1];
> Mask=convn(logical(A),K,'same')>0;
>
> B=A;
> B(Mask)=1-B(Mask),

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