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:
replace element values according to indexes

Subject: replace element values according to indexes

From: Eric

Date: 4 Mar, 2011 16:16:10

Message: 1 of 5

I would like to do this, let say I have a matrix M, I want to get the max value of each row and replace the max value in M with 0. Could it be done without any loop?
[mx, Ioc] = max(M, [], 2);

How to change the values of M according to Ioc? Thanks.

Subject: replace element values according to indexes

From: someone

Date: 4 Mar, 2011 16:31:05

Message: 2 of 5

"Eric" wrote in message <ikr38a$s62$1@fred.mathworks.com>...
> I would like to do this, let say I have a matrix M, I want to get the max value of each row and replace the max value in M with 0. Could it be done without any loop?
> [mx, Ioc] = max(M, [], 2);
>
> How to change the values of M according to Ioc? Thanks.

M = [1 2 3;4 5 6;7 8 9];
[mx, Ioc] = max(M, [], 2);
M(Ioc,:) = NaN

M =

     1 2 3
     4 5 6
   NaN NaN NaN

Subject: replace element values according to indexes

From: Eric

Date: 4 Mar, 2011 16:50:18

Message: 3 of 5

"someone" wrote in message <ikr449$mqk$1@fred.mathworks.com>...
> "Eric" wrote in message <ikr38a$s62$1@fred.mathworks.com>...
> > I would like to do this, let say I have a matrix M, I want to get the max value of each row and replace the max value in M with 0. Could it be done without any loop?
> > [mx, Ioc] = max(M, [], 2);
> >
> > How to change the values of M according to Ioc? Thanks.
>
> M = [1 2 3;4 5 6;7 8 9];
> [mx, Ioc] = max(M, [], 2);
> M(Ioc,:) = NaN
>
> M =
>
> 1 2 3
> 4 5 6
> NaN NaN NaN

Thanks for the reply. However you had get me wrong.
M I'm looking for should be
1 2 NaN
4 5 NaN
7 8 NaN

i.e.
>> M = [1 9 3;8 5 6;7 8 9]

M =

     1 9 3
     8 5 6
     7 8 9

>> [mx, loc] = max(M, [], 2)

mx =

     9
     8
     9


loc =

     2
     1
     3

let say I would like to set the maximum number to zero. According to loc, M should be
M =

     1 0 3
     0 5 6
     7 8 0

Subject: replace element values according to indexes

From: Bruno Luong

Date: 4 Mar, 2011 17:36:04

Message: 4 of 5

"Eric" wrote in message <ikr58a$5vi$1@fred.mathworks.com>...
> "someone" wrote in message <ikr449$mqk$1@fred.mathworks.com>...

> let say I would like to set the maximum number to zero. According to loc, M should be
> M =
>
> 1 0 3
> 0 5 6
> 7 8 0

 M(sub2ind(size(M),(1:size(M,1))',loc)) = 0

% Bruno

Subject: replace element values according to indexes

From: Eric

Date: 4 Mar, 2011 17:43:05

Message: 5 of 5

Hi Bruno, you again that answer me the question. I have posted you a question in file exchange, regarding the installation of minmaxk. Thank you very much.

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ikr7u4$pgv$1@fred.mathworks.com>...
> "Eric" wrote in message <ikr58a$5vi$1@fred.mathworks.com>...
> > "someone" wrote in message <ikr449$mqk$1@fred.mathworks.com>...
>
> > let say I would like to set the maximum number to zero. According to loc, M should be
> > M =
> >
> > 1 0 3
> > 0 5 6
> > 7 8 0
>
> M(sub2ind(size(M),(1:size(M,1))',loc)) = 0
>
> % Bruno

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