[Jprogramming] Array Coordinates
'robert therriault' via Programming
programming at jsoftware.com
Fri Nov 30 19:41:40 UTC 2018
I came up with
($ #: (# i.@:#)@:,)
and then while I was checking the efficiency Raul came up with ($ #: (# i.@:#)@:,) which it turns out is even quicker, although I do like the elegance of Don's approach. Since Skip was wondering for multiple dimensions as well, I tested for 3 dimensions as well.
Results follow.
Cheers, bob
a=.10 100ドル=23|i.100
b=.5 5 100ドル=23|i.250
IDa=: $ ((] <.@% 1{[) <"1@,. ] |~ 1 { [) I.@, NB. Pascal
IDb=: (#&, {@:(i.&.>)@$) NB. Roger
IDc=: 4 $. $. NB. Don
IDd=: $ #: (# i.@:#)@:, NB. Bob
IDe=:$#:I.@, NB. Raul
IDa a
┌───┬───┬───┬───┬───┐
│0 0│2 3│4 6│6 9│9 2│
└───┴───┴───┴───┴───┘
IDb a
┌───┬───┬───┬───┬───┐
│0 0│2 3│4 6│6 9│9 2│
└───┴───┴───┴───┴───┘
IDc a
0 0
2 3
4 6
6 9
9 2
IDd a
0 0
2 3
4 6
6 9
9 2
IDe a
0 0
2 3
4 6
6 9
9 2
IDa b NB. limited to 2D
┌───┬───┬───┬────┬────┬────┬────┬────┬────┬────┬────┐
│0 0│4 3│9 1│13 4│18 2│23 0│27 3│32 1│36 4│41 2│46 0│
└───┴───┴───┴────┴────┴────┴────┴────┴────┴────┴────┘
IDb b
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│0 0 0│0 2 3│0 4 6│1 1 9│1 4 2│2 1 5│2 3 8│3 1 1│3 3 4│4 0 7│4 3 0│
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
IDc b
0 0 0
0 2 3
0 4 6
1 1 9
1 4 2
2 1 5
2 3 8
3 1 1
3 3 4
4 0 7
4 3 0
IDd b
0 0 0
0 2 3
0 4 6
1 1 9
1 4 2
2 1 5
2 3 8
3 1 1
3 3 4
4 0 7
4 3 0
IDe b
0 0 0
0 2 3
0 4 6
1 1 9
1 4 2
2 1 5
2 3 8
3 1 1
3 3 4
4 0 7
4 3 0
1000 timespacex 'IDa a'
1.059e_6 2560
1000 timespacex 'IDb a'
4.038e_6 16128
1000 timespacex 'IDc a'
1.006e_6 3392
1000 timespacex 'IDd a'
6.89e_7 1536
1000 timespacex 'IDe a'
6.87e_7 1536
1000 timespacex 'IDb b'
8.861e_6 38400
1000 timespacex 'IDc b'
1.264e_6 3776
1000 timespacex 'IDd b'
1.431e_6 1920
1000 timespacex 'IDe b'
1.358e_6 1920
JVERSION
Engine: j807/j64/darwin
Release: commercial/2018-10-05T11:55:35
Library: 8.07.20
Qt IDE: 1.7.9/5.9.6
Platform: Darwin 64
Installer: J807 install
InstallPath: /users/bobtherriault/j64-807
Contact: www.jsoftware.com
> On Nov 30, 2018, at 11:31 AM, Raul Miller <rauldmiller at gmail.com> wrote:
>> Or, slightly longer:
> ($a)#:I.,a
> or
> ($#:I.@,)a
>> ...
>> Thanks,
>> --
> Raul
>> On Fri, Nov 30, 2018 at 2:04 PM Don Guinn <donguinn at gmail.com> wrote:
>>>> Or do
>>>> 4 $.$.a
>>>> On Fri, Nov 30, 2018, 11:56 AM Roger Hui <rogerhui.canada at gmail.com wrote:
>>>>> a #&, {i.&.>$a
>>> ┌───┬───┬───┬───┬───┐
>>> │0 0│2 3│4 6│6 9│9 2│
>>> └───┴───┴───┴───┴───┘
>>>> a #&, {i.&.>$a
>>> 0 0
>>> 2 3
>>> 4 6
>>> 6 9
>>> 9 2
>>>>>>>>>>>> On Fri, Nov 30, 2018 at 10:52 AM Skip Cave <skip at caveconsulting.com>
>>> wrote:
>>>>>>> Given the array a:
>>>>>>>> ]a=.10 100ドル=23|i.100
>>>>>>>> 1 0 0 0 0 0 0 0 0 0
>>>>>>>> 0 0 0 0 0 0 0 0 0 0
>>>>>>>> 0 0 0 1 0 0 0 0 0 0
>>>>>>>> 0 0 0 0 0 0 0 0 0 0
>>>>>>>> 0 0 0 0 0 0 1 0 0 0
>>>>>>>> 0 0 0 0 0 0 0 0 0 0
>>>>>>>> 0 0 0 0 0 0 0 0 0 1
>>>>>>>> 0 0 0 0 0 0 0 0 0 0
>>>>>>>> 0 0 0 0 0 0 0 0 0 0
>>>>>>>> 0 0 1 0 0 0 0 0 0 0
>>>>>>>>>>>> How can I write a function 'ac' that will list the coordinates of each 1
>>> in
>>>> the array?
>>>>>>>> Example using array a:
>>>>>>>> ac a
>>>>>>>> 0 0
>>>>>>>> 2 3
>>>>>>>> 4 6
>>>>>>>> 6 9
>>>>>>>> 9 3
>>>>>>>> Extend to 3 or more dimensions?
>>>>>>>> Skip Cave
>>>> ----------------------------------------------------------------------
>>>> 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
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list