Re: Find index of maximal element in multi-dimensional array
- To: mathgroup at smc.vnet.net
- Subject: [mg73517] Re: Find index of maximal element in multi-dimensional array
- From: "Valeri Astanoff" <astanoff at gmail.com>
- Date: Wed, 21 Feb 2007 01:39:39 -0500 (EST)
- References: <erelbt$7h4$1@smc.vnet.net>
On 20 f=E9v, 12:17, "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
Good day,
Compare these two solutions :
In[1]:=z = Table[Random[], {500}, {600}] ;
In[2]:={{r,c}=Dimensions[z];{i,j}={n=Ordering[z//Flatten,1,Greater]
[[1]];
Floor[n/c]+1,Mod[n,c]},z[[i,j]]}//Timing
Out[2]={0.203 Second,{{211,206},0.999998}}
In[3]:=Last[Sort[Flatten[MapIndexed[{#2, #1} & , z, {2}], 1],
OrderedQ[{#1[[2]], #2[[2]]}] & ]] //Timing
Out[3]={19.063 Second,{{211,206},0.999998}}
v=2Ea.