Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you replace the first nonzero element in each row with -1 ?

3 views
Skip to first unread message

douglasg...@gmail.com

unread,
May 3, 2009, 7:27:39 PM5/3/09
to
Trying to figure out how to do two things.. any help is very much
appreciated

1) I want to replace the first nonzero element of each row of a matrix
with -1

Example

5 0 10
0 3 7
0 6 0

Becomes..

-1 0 10
0 -1 7
0 -1 0

2) Is there any way to do a multiplication like the one below.. where
all of the elements of row 1 in Matrix B are multiplied by element 1
of vector A.... all row 2 elements in B are multiplied by element 2 in
A, etc...

Example...

A x B = C
1 1 2 1 2
2 1 2 2 4
3 1 2 3 6

Thank you!

Nasser Abbasi

unread,
May 3, 2009, 7:53:28 PM5/3/09
to

<douglasg...@gmail.com> wrote in message
news:978e2da1-56a0-45e3...@f1g2000prb.googlegroups.com...

> Trying to figure out how to do two things.. any help is very much
> appreciated
>
> 1) I want to replace the first nonzero element of each row of a matrix
> with -1
>
> Example
>
> 5 0 10
> 0 3 7
> 0 6 0
>
> Becomes..
>
> -1 0 10
> 0 -1 7
> 0 -1 0
>

K>> A=[5 0 10;
0 3 7;
0 6 0];
A(find(A~=0,size(A,1)))=-1

A =

-1 0 10
0 -1 7
0 -1 0


> 2) Is there any way to do a multiplication like the one below.. where
> all of the elements of row 1 in Matrix B are multiplied by element 1
> of vector A.... all row 2 elements in B are multiplied by element 2 in
> A, etc...
>
> Example...
>
> A x B = C
> 1 1 2 1 2
> 2 1 2 2 4
> 3 1 2 3 6
>
>
>
> Thank you!

K>> A

A =

1
2
3

K>> B

B =

1 2
1 2
1 2

K>> cell2mat(arrayfun(@(i) A(:).*B(:,i),[1:2],'UniformOutput',false))

ans =

1 2
2 4
3 6

K>>

--Nasser


Message has been deleted

douglasgree...@gmail.com

unread,
May 3, 2009, 8:58:17 PM5/3/09
to
On May 3, 4:53 pm, "Nasser Abbasi" <n...@12000.org> wrote:
> <douglasgreenb...@gmail.com> wrote in message

Wow... THanks so much! Small problem - the method of replacing the
first nonzero elements with -1 worked, but not quite 100% of the
time. (the other code worked perfectly though)

For instance, in the matrix below..

50 75 0
50 0 100
0 75 100
75 100 0
0 75 100

The formula resulted in


-1 75 0
-1 0 100
0 -1 100
-1 100 0
0 75 100

For some reason, the 75 in the last row was not converted..

I'm not sure why

Doug


Matt Fig

unread,
May 3, 2009, 9:24:01 PM5/3/09
to
For the second problem, use bsxfun.

A =
1
2
3

B =


1 2
1 2
1 2


bsxfun(@times,A,B)

Nasser Abbasi

unread,
May 3, 2009, 9:28:52 PM5/3/09
to

<douglasgree...@gmail.com> wrote in message
news:3ff8e36b-8026-4f61...@u9g2000pre.googlegroups.com...

On May 3, 4:53 pm, "Nasser Abbasi" <n...@12000.org> wrote:
> <douglasgreenb...@gmail.com> wrote in message
>

" Small problem - the method of replacing the


first nonzero elements with -1 worked, but not quite 100% of the
time. (the other code worked perfectly though)

For instance, in the matrix below..

50 75 0
50 0 100
0 75 100
75 100 0
0 75 100

The formula resulted in


-1 75 0
-1 0 100
0 -1 100
-1 100 0
0 75 100

For some reason, the 75 in the last row was not converted..

I'm not sure why

Doug"

There is a bug :)

I'll fix it soon. I am making coffee now, please wait a minute

--Nasser

Message has been deleted
Message has been deleted

Nasser Abbasi

unread,
May 3, 2009, 9:52:58 PM5/3/09
to

"Nasser Abbasi" <n...@12000.org> wrote in message
news:ODrLl.15990$pr6....@flpi149.ffdc.sbc.com...


Ok, coffee is done :), here it is

EDU>> clear all

A=[50 75 0;


50 0 100;
0 75 100;
75 100 0;

0 75 100;
0 75 100];

[I,J]=ind2sub(size(A),find(A~=0));
[b,c]=unique(I,'first');
A(sub2ind(size(A),b,J(c)))=-1

A =

-1 75 0
-1 0 100
0 -1 100
-1 100 0

0 -1 100
0 -1 100

EDU>>


douglasg...@gmail.com

unread,
May 3, 2009, 9:59:47 PM5/3/09
to
On May 3, 6:52 pm, "Nasser Abbasi" <n...@12000.org> wrote:
> "Nasser Abbasi" <n...@12000.org> wrote in message
>
> news:ODrLl.15990$pr6....@flpi149.ffdc.sbc.com...
>
>
>
>
>
> > <douglasgreenbergsig...@gmail.com> wrote in message

BINGO! That did it. No idea how it did it. But it did it. Thank
you sir. What do I owe you?

THanks also Jean-Claude... I tried yours but couldn't get it to work
(which is almost certainly my / the operator's fault).

Message has been deleted

Bruno Luong

unread,
May 4, 2009, 3:18:01 AM5/4/09
to
If Matlab does not have first option for UNIQUE. Here is one way:

B=cumsum(A~=0,2)>0
B=[false(size(B,1),1) B]
A(logical(diff(B,1,2))) = -1

% Bruno

douglasgree...@gmail.com

unread,
May 4, 2009, 11:44:40 PM5/4/09
to
On May 4, 12:18 am, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:

That works too - thanks!

Jos

unread,
May 5, 2009, 3:35:02 AM5/5/09
to
douglasgree...@gmail.com wrote in message <aa4957eb-73ac-40f5...@i28g2000prd.googlegroups.com>...
> On May 4, 12:18?am, "Bruno Luong" <b.lu...@fogale.findmycountry>

What about the one-liner:

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

Jos

Nasser Abbasi

unread,
May 5, 2009, 4:12:29 AM5/5/09
to

"Jos " <#10...@fileexchange.com> wrote in message
news:gtoq76$bn0$1...@fred.mathworks.com...

Nice.

When working with matrices and vectors, Matlab really is very powerfull.

--Nasser


Bruno Luong

unread,
May 5, 2009, 6:00:18 AM5/5/09
to
"Jos " <#10...@fileexchange.com> wrote in message <gtoq76$bn0$1...@fred.mathworks.com>...

> What about the one-liner:
>

Nice !

Bruno

Message has been deleted
0 new messages