Fifty Shades of J part 11, amend selection by criterion question

46 views
Skip to first unread message

Daniel _

unread,
Mar 4, 2026, 4:32:36 PM (2 days ago) Mar 4
to forum
The following is shown as an example to amend with arbitrary indices:
   ]p=:3 5$i.3
0 1 2 0 1 
2 0 1 2 0 
1 2 0 1 2

   (=&0)p 
1 0 0 1 0 
0 1 0 0 1 
0 0 1 0 0

   (i.$p)*(=&0)p 
0 0   0 3 0 
0 6   0 0 9 
0 0 12 0 0

   99((i.@$*=&0)@])}p 
99   1   2 99   1 
  2 99   1   2 99 
  1   2 99   1   2

Unfortunately (I think) the presentation of the indices within what looks like a mask is confusing, and doesn't hint at what happens when replacing anything other than zeros:

      99((i.@$*=&1)@])}p
99 99   2   0 99
  2   0 99   2   0
99   2   0 99   2

My question is, is this intended? if not, what is the idiomatic way to convert a mask to a list of indices for an N-dimensional array?

Joshua Y

unread,
Mar 4, 2026, 7:06:30 PM (2 days ago) Mar 4
to forum, Daniel _
I'm not sure about what's idiomatic, but for this example I'd be inclined to go for this
   99 (1 I.@,@:= ])} p      NB. or ([: I.@, 1=]) if you prefer

 0 99  2  0 99
 2  0 99  2  0
99  2  0 99  2

In terms of how to idiomatically convert a mask to a noun of 2D indices, I think it's usually ($ #: I.@,) or the less obvious (4 $. $.)

   ] k =: (4 $. $.) 1 = p
0 1
0 4
1 2
2 0
2 3
   99 k} p

 0 99  2  0 99
 2  0 99  2  0
99  2  0 99  2

Daniel _

unread,
Mar 4, 2026, 7:58:29 PM (2 days ago) Mar 4
to forum, Joshua Y, Daniel _
That makes a lot of sense, thank you!
Reply all
Reply to author
Forward
0 new messages