Re: Find index of maximal element in multi-dimensional
- To: mathgroup at smc.vnet.net
- Subject: [mg73544] Re: [mg73492] Find index of maximal element in multi-dimensional
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Wed, 21 Feb 2007 01:54:14 -0500 (EST)
- Reply-to: hanlonr at cox.net
z = Table[Random[], {5}, {6}] ;
Last[Sort[Flatten[MapIndexed[{#2, #1} & , z, {2}], 1],
OrderedQ[{#1[[2]], #2[[2]]}] & ]]
{{4,6},0.94111}
{Position[z,#][[1]],#}&[Max[z]]
{{4,6},0.94111}
Your approach does not work for arrays of higher dimension
z = Table[Random[], {5}, {6},{4}] ;
Last[Sort[Flatten[MapIndexed[{#2, #1} & , z, {2}], 1],
OrderedQ[{#1[[2]], #2[[2]]}] & ]]
{{5,5},{0.890354,0.0253132,0.877603,0.981199}}
{Position[z,#][[1]],#}&[Max[z]]
{{5,1,3},0.998183}
Bob Hanlon
---- Andrew Moylan <andrew.j.moylan 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