Re: Find index of maximal element in multi-dimensional array
- To: mathgroup at smc.vnet.net
- Subject: [mg73548] Re: Find index of maximal element in multi-dimensional array
- From: Oliver Ruebenkoenig <ruebenko at uni-freiburg.de>
- Date: Wed, 21 Feb 2007 01:56:27 -0500 (EST)
- References: <erelbt$7h4$1@smc.vnet.net>
Hi, how about n1 = n2 = 1000; z = Table[Random[], {n1}, {n2}]; f2 = Compile[ {{z, _Real, 2 }}, Position[z, Max[z] ] ]; (res = f2[z]) // Timing z[[ Sequence @@ res[[1]] ]] - Max[z] Oliver On Tue, 20 Feb 2007, 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 > > > Oliver Ruebenkoenig, <ruebenko AT uni-freiburg.de>