MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Find index of maximal element in multi-dimensional array

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73508] Re: Find index of maximal element in multi-dimensional array
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 21 Feb 2007 01:34:50 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <erelbt$7h4$1@smc.vnet.net>

Andrew Moylan wrote:
> Hi all,
> 
> Here's a two-dimensional array of numbers:
> 
> z = Table[Random[], {5}, {6}]
> 
> What's an efficient way of finding the index of the maximal element of
> z? Here's one way:
> 
> Last[Sort[Flatten[MapIndexed[{#2, #1} & , z, {2}], 1],
>    OrderedQ[{#1[[2]], #2[[2]]}] & ]]
> 
> The output is like this:
> 
> {{5, 4}, 0.921344}
> 
> But it's so untidy. Any better ideas?
> 
> Cheers,
> 
> Andrew
> 
> 
What about using Position?

In[1]:=
z = Table[Random[], {5}, {6}]

Out[1]=
{{0.614105,0.694755,0.330049,0.387635,0.62543,0.418811},
{0.70748,0.421158,0.382081,0.0055536,0.460341,0.479205},
{0.254711,0.137264,0.15499,0.535576,0.0582237,0.124846},
{0.557463,0.129568,0.212017,0.770145,0.825369,0.676279},
{0.597912,0.0753907,0.49532,0.288645,0.972482,0.65658}}

In[2]:=
posmax[z_] := Module[{m = Max[z]}, {Position[z, x_ /; x == m][[1]], m}]

In[3]:=
posmax[z]

Out[3]=
{{5,5},0.972482}

Regards,
Jean-Marc


  • Prev by Date: Re: Find index of maximal element in multi-dimensional array
  • Next by Date: Re: Find index of maximal element in multi-dimensional
  • Previous by thread: Re: Find index of maximal element in multi-dimensional array
  • Next by thread: Re: Find index of maximal element in multi-dimensional array