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:
How to NOT get the first element as the max when elements are equal

Subject: How to NOT get the first element as the max when elements are equal

From: Yildiray Yildiz

Date: 10 May, 2012 23:28:55

Message: 1 of 8

Hello,

I have the following code:

[~,ind]=max(Defender.Q,[],6);

Defender.Q is a HUGE multidimensional matrix.

When there are multiple maximums in the 6th dimension of Defender.Q, the max function
is giving me the index of the first of these multiple maximums. I want to get an index
that is randomized between multiple maximums. Any ideas? Thanks for your help!

Subject: How to NOT get the first element as the max when elements are

From: dpb

Date: 11 May, 2012 00:57:51

Message: 2 of 8

On 5/10/2012 6:28 PM, Yildiray Yildiz wrote:
> Hello,
>
> I have the following code:
>
> [~,ind]=max(Defender.Q,[],6);
>
> Defender.Q is a HUGE multidimensional matrix.
>
> When there are multiple maximums in the 6th dimension of Defender.Q, the
> max function
> is giving me the index of the first of these multiple maximums. I want
> to get an index
> that is randomized between multiple maximums. Any ideas? Thanks for your
> help!

Do a RANDPERM on the length of the returned indices, select the first
afterward.

--

Subject: How to NOT get the first element as the max when elements are equal

From: Roger Stafford

Date: 11 May, 2012 01:45:37

Message: 3 of 8

"Yildiray Yildiz" wrote in message <johivn$ni4$1@newscl01ah.mathworks.com>...
> [~,ind]=max(Defender.Q,[],6);
>
> Defender.Q is a HUGE multidimensional matrix.
>
> When there are multiple maximums in the 6th dimension of Defender.Q, the max function
> is giving me the index of the first of these multiple maximums. I want to get an index
> that is randomized between multiple maximums. Any ideas? Thanks for your help!
- - - - - - - - - - -
  Let Q be your "huge" array. See if the following works for you:

 m = max(Q,[],6);
 t = bsxfun(@eq,m,Q); % Identify all multiple maxima along each dim 6
 f = find(t); % Get pointers to all of them
 t(f) = rand(length(f),1); % Multiply each 'TRUE' by a random number
 [~,ind] = max(t,[],6); % Find the maximums of these random numbers

Roger Stafford

Subject: How to NOT get the first element as the max when elements are

From: Yildiray Yildiz

Date: 11 May, 2012 05:02:24

Message: 4 of 8

dpb <none@non.net> wrote in message <joho6f$tj4$1@speranza.aioe.org>...

> Do a RANDPERM on the length of the returned indices, select the first
> afterward.
>
Thanks for your answer. Actually part of the problem is finding those multiple indexes.

Subject: How to NOT get the first element as the max when elements are equal

From: Yildiray Yildiz

Date: 11 May, 2012 05:05:40

Message: 5 of 8

"Roger Stafford" wrote in message <johr01$o50$1@newscl01ah.mathworks.com>...
> "Yildiray Yildiz" wrote in message <johivn$ni4$1@newscl01ah.mathworks.com>...
> > [~,ind]=max(Defender.Q,[],6);
> >
> > Defender.Q is a HUGE multidimensional matrix.
> >
> > When there are multiple maximums in the 6th dimension of Defender.Q, the max function
> > is giving me the index of the first of these multiple maximums. I want to get an index
> > that is randomized between multiple maximums. Any ideas? Thanks for your help!
> - - - - - - - - - - -
> Let Q be your "huge" array. See if the following works for you:
>
> m = max(Q,[],6);
> t = bsxfun(@eq,m,Q); % Identify all multiple maxima along each dim 6
> f = find(t); % Get pointers to all of them
> t(f) = rand(length(f),1); % Multiply each 'TRUE' by a random number
> [~,ind] = max(t,[],6); % Find the maximums of these random numbers
>
> Roger Stafford

Excellent! Thank you, Roger. One note: For some reason t(f) = rand(length(f),1) did not work.
I had to do:

t = t*rand;
t(f) = rand(length(f),1);

Although I do not know why I had to modify the "t" matrix first, it worked.

Thank you again.

Subject: How to NOT get the first element as the max when elements are equal

From: Roger Stafford

Date: 11 May, 2012 05:35:25

Message: 6 of 8

"Yildiray Yildiz" wrote in message <joi6n4$9oa$1@newscl01ah.mathworks.com>...
> Excellent! Thank you, Roger. One note: For some reason t(f) = rand(length(f),1) did not work.
> I had to do:
>
> t = t*rand;
> t(f) = rand(length(f),1);
>
> Although I do not know why I had to modify the "t" matrix first, it worked.
- - - - - - - - -
  The "t(f) = rand(length(f),1);" line works on my matlab version without modifying t first, but it is a very old version. Probably the "t = t*rand;" simply serves to convert 't' from a logical array to a numerical one with 1's and 0's. If so, you can probably just do "t = t+0" or "t = double(t)" instead.

Roger Stafford

Subject: How to NOT get the first element as the max when elements are equal

From: Roger Stafford

Date: 11 May, 2012 05:48:14

Message: 7 of 8

"Roger Stafford" wrote in message <joi8et$g99$1@newscl01ah.mathworks.com>...
> ..... Probably the "t = t*rand;" simply serves to convert 't' from a logical array to a numerical one with 1's and 0's.
- - - - - - - -
  I meant to say, "with zeros and nonzeros".

Roger Stafford

Subject: How to NOT get the first element as the max when elements are equal

From: Yildiray Yildiz

Date: 11 May, 2012 06:05:41

Message: 8 of 8

"Roger Stafford" wrote in message <joi96u$itt$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <joi8et$g99$1@newscl01ah.mathworks.com>...
> > ..... Probably the "t = t*rand;" simply serves to convert 't' from a logical array to a numerical one with 1's and 0's.
> - - - - - - - -
> I meant to say, "with zeros and nonzeros".
>
> Roger Stafford

Yep, makes sense. Thanks again Roger! Saved me a lot of time.

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