[Rd] arr.ind argument to which.min and which.max
Henrik Bengtsson
hb at stat.berkeley.edu
Mon Jul 5 22:53:59 CEST 2010
...and, of course, just after sending it off I found out that from R
v2.11.0 there is now an arrayInd() in the 'base' package doing exactly
the same thing. See help(arrayInd).
/Henrik
On Mon, Jul 5, 2010 at 10:49 PM, hb <hb at stat.berkeley.edu> wrote:
> For what it's worth, see arrayIndex() in R.utils, e.g.
>
> # Single index
> print(arrayIndex(21, dim=c(4,3,3)))
>
> # Multiple indices
> print(arrayIndex(20:23, dim=c(4,3,3)))
>
> # Whole array
> x <- array(1:30, dim=c(5,6))
> print(arrayIndex(1:length(x), dim=dim(x)))
>
> # Find (row,column) of maximum value
> m <- diag(4-abs(-4:4))
> print(arrayIndex(which.max(m), dim=dim(m)))
>
> /Henrik
>
> On Mon, Jul 5, 2010 at 8:27 PM, Patrick Burns <pburns at pburns.seanet.com> wrote:
>> On 05/07/2010 10:56, Martin Maechler wrote:
>>>>>>>>
>>>>>>>> "PatB" == Patrick Burns<pburns at pburns.seanet.com>
>>>>>>>> on Sun, 04 Jul 2010 09:43:44 +0100 writes:
>>>
>>> PatB> Is there a reason that 'which.min' and
>>> PatB> 'which.max' don't have an 'arr.ind'
>>> PatB> argument?
>>>
>>> well, help(which.min) tells you that they really were aimed at
>>> doing their job *fast* for vectors.
>>>
>>> Of course you are right and a generalization to arrays might be
>>> convenient at times.
>>>
>>> PatB> The context in which I wanted that was
>>> PatB> a grid search optimization, which seems
>>> PatB> like it would be reasonably common to me.
>>>
>>> well, as the author of these two functions, I can only say
>>>
>>> "patches are welcome!"
>>>
>>> and I think should be pretty simple, right ?
>>> You just have to do very simple remapping of the 1d index 'i' back
>>> to the array index, i.e., the same operation
>>> you need to transform seconds into days:hours:minutes:seconds
>>> {{and yes, we old-timers may recall that APL had an operator (I
>>> think "T-bar") to do that ...}
>>
>> I think the exercise is just to copy the definition of
>> 'which' and add four characters.
>>
>> If the order of the if condition were reversed, then
>> possibly the slight reduction in speed of 'which.min'
>> and 'which.max' would be more than made up for in the
>> slight increase in speed of 'which'.
>>
>> Pat
>>
>>>
>>> Martin Maechler, ETH Zurich
>>>
>>>
>>> PatB> --
>>> PatB> Patrick Burns
>>> PatB> pburns at pburns.seanet.com
>>> PatB> http://www.burns-stat.com
>>> PatB> (home of 'Some hints for the R beginner'
>>> PatB> and 'The R Inferno')
>>>
>>
>> --
>> Patrick Burns
>> pburns at pburns.seanet.com
>> http://www.burns-stat.com
>> (home of 'Some hints for the R beginner'
>> and 'The R Inferno')
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>
More information about the R-devel
mailing list