Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg73556] Re: Find index of maximal element in multi-dimensional array
  • From: "Andrew Moylan" <andrew.j.moylan at gmail.com>
  • Date: Wed, 21 Feb 2007 06:07:08 -0500 (EST)
  • References: <erelbt$7h4$1@smc.vnet.net><ergrcj$jql$1@smc.vnet.net>

On Feb 21, 6:12 pm, Jens-Peer Kuska <k... at informatik.uni-leipzig.de>
wrote:
> Hi,
>
> Position[z, Max[Flatten[z]]]
>
> ??
>
> 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

Thanks for your replies. Would you believe I wrote that crazy looking
expression in my first post because I didn't want Mathematica to
traverse the list twice? I somehow didn't notice that the code above
has to traverse it MORE than twice, owing to a call to MapIndexed (one
traversal) and a call to Sort (a second traversal, and then some).

Anyway, clearly Position[z, Max[z]] is better. But does anyone have
any neat ideas for a method that will only traverse the list once, as
(I assume) Max[ ] does?



  • Prev by Date: Re: Showing that ArcSinh[2]/ArcCsch[2] is 3?
  • Next by Date: ToMatlab limitations
  • 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