Re: Find index of maximal element in multi-dimensional array
- To: mathgroup at smc.vnet.net
- Subject: [mg73524] Re: Find index of maximal element in multi-dimensional array
- From: "Ray Koopman" <koopman at sfu.ca>
- Date: Wed, 21 Feb 2007 01:43:25 -0500 (EST)
- References: <erelbt$7h4$1@smc.vnet.net>
On Feb 20, 3:17 am, "Andrew Moylan" <andrew.j.moy... at gmail.com> 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 z = Table[Random[],{5},{n = 6}] {{0.247831, 0.85266, 0.87098, 0.840725, 0.808299, 0.160707}, {0.692361, 0.613514,0.758175, 0.823928, 0.0331389,0.330202}, {0.155955, 0.89171, 0.770831, 0.327228, 0.974979, 0.25434}, {0.325965, 0.5732, 0.388035, 0.0842301,0.089699, 0.99669}, {0.140205, 0.23157, 0.218719, 0.155965, 0.331906, 0.0708631}} {Ceiling[#/n],Mod[#,n,1]}&@@Ordering[Flatten@z,-1] {4,6}