plus, Henry’s solution looks more idiomatic (too me, at least)
usually, I try to keep related structures aligned (using grade)
other than that, I hardly remember any cases when linear indexing
didn’t do the job, maybe after some reduction
so I think in most cases this should not be needed
but if so, I’d take Henry’s approach
Am 13.11.20 um 18:40 schrieb 'robert therriault' via Programming:
> Having seen Henry's solution, I see that I have left the job half done, so
> here is the rest of my solution.
>
> dyadind =. ;@:(= #"1 {@({@i."0 @$@]))
> 6 dyadind i. 3 4
> 1 2
>
> Having produced that, I note that Henry's is twice as fast and takes up half
> the space and the differences become more apparent at higher dimensions.
>
> 1000 timespacex '6 dyadind i. 3 4'
> 1.807e_6 4288
> 1000 timespacex '6 ($@] #: I.@(= ,)) i. 3 4'
> 8.68e_7 2496
> 1000 timespacex '6 dyadind i. 2 3 3 4'
> 5.283e_6 12992
> 1000 timespacex '6 ($@] #: I.@(= ,)) i. 2 3 3 4'
> 1.05e_6 3008
>
> Cheers, bob
>
>> On Nov 13, 2020, at 09:25, 'robert therriault' via Programming
>> <[email protected]> wrote:
>>
>> Hi Thomas,
>>
>> I usually use something like this which relies on Category ({) to group the
>> indices generated by i. applied to each index of the shape. I imagine others
>> have different approaches.
>>
>> i. 3 4
>> 0 1 2 3
>> 4 5 6 7
>> 8 9 10 11
>> ind=.{@({@i."0 @$)
>> ind i. 3 4
>> ┌───┬───┬───┬───┐
>> │0 0│0 1│0 2│0 3│
>> ├───┼───┼───┼───┤
>> │1 0│1 1│1 2│1 3│
>> ├───┼───┼───┼───┤
>> │2 0│2 1│2 2│2 3│
>> └───┴───┴───┴───┘
>> i. 2 3 4
>> 0 1 2 3
>> 4 5 6 7
>> 8 9 10 11
>>
>> 12 13 14 15
>> 16 17 18 19
>> 20 21 22 23
>> ind i. 2 3 4
>> ┌─────┬─────┬─────┬─────┐
>> │0 0 0│0 0 1│0 0 2│0 0 3│
>> ├─────┼─────┼─────┼─────┤
>> │0 1 0│0 1 1│0 1 2│0 1 3│
>> ├─────┼─────┼─────┼─────┤
>> │0 2 0│0 2 1│0 2 2│0 2 3│
>> └─────┴─────┴─────┴─────┘
>>
>> ┌─────┬─────┬─────┬─────┐
>> │1 0 0│1 0 1│1 0 2│1 0 3│
>> ├─────┼─────┼─────┼─────┤
>> │1 1 0│1 1 1│1 1 2│1 1 3│
>> ├─────┼─────┼─────┼─────┤
>> │1 2 0│1 2 1│1 2 2│1 2 3│
>> └─────┴─────┴─────┴─────┘
>>
>> Cheers, bob
>>
>>> On Nov 13, 2020, at 09:05, thomas.bulka via Programming
>>> <[email protected]> wrote:
>>>
>>> Hi everyone,
>>>
>>> still learning J, I stumbled across a problem which should be easy to
>>> solve, but somehow I have not been able to do so, yet. Say, I have defined
>>> a matrix M:
>>>
>>> M =: 3 4 $ >: i. 12
>>>
>>> I now want to get the row and column index of 6 in M, which is 1 2. If M
>>> was a vector, I could easily get the index of 6 with the help of I.:
>>>
>>> I. 6 = , M
>>>
>>> Applying I. to M in its original shape, however, returns a boolean vector
>>> which indicates in which rows 6 has been found (at least that is my
>>> interpretation).
>>>
>>> Is it possible to apply I. to higher rank arrays in order to receive the
>>> complete indices of the elements I'm looking for? Or should I approach that
>>> problem in another way?
>>>
>>> Thank you very much in advance for your help!
>>>
>>> Kind regards,
>>>
>>> Thomas
>>>
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
----------------------
mail written using NEO
neo-layout.org
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm