Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define selection using index function in J

Tags:

j

Let's assume I have a following tensor t:

   ]m=: 100 + 4 4 $ i.16
100 101 102 103
104 105 106 107
108 109 110 111
112 113 114 115
   ]t=: (m ,: m+100) , m+200
100 101 102 103
104 105 106 107
108 109 110 111
112 113 114 115

200 201 202 203
204 205 206 207
208 209 210 211
212 213 214 215

300 301 302 303
304 305 306 307
308 309 310 311
312 313 314 315

I would like to select diagonal plane of it, so :

100 105 110 115

200 205 210 215

300 305 310 315

How to define function that acts on indices? (and here have for any plane index let's choose ix(row) = ix (column)) Also, how to define functions working on values and indices together? So I would be interested in having something like this:

(f t) { t

Thanks!

like image 726
paweljakubas Avatar asked Aug 01 '26 12:08

paweljakubas


2 Answers

Transpose x|:y with boxed arguments runs the axes together to produce a single axis. You can use this to produce a rather idiomatic solution:

(< 0 1) |: m
100 105 110 115

(<0 1) |:"2 t
100 105 110 115
200 205 210 215
300 305 310 315

where you use the rank " verb to apply the diagonal selection to 2-boxes.

like image 84
Eelvex Avatar answered Aug 03 '26 01:08

Eelvex


You can convert an array of values to its corresponding array of indices with (#:i.)@$ m

To get an example f „working on values and indices together“ you can then plug it in as a dyad that takes values on the left and indices on the right:

  f=.(2|[) +. ([:=/"1]) NB. odd value or diagonal index
  ]r=.([ f (#:i.)@$) m  NB. values f indices
1 1 0 1
0 1 0 1
0 1 1 1
0 1 0 1
  r #&, m               NB. flatten lists & get values where bit is set
100 101 103 105 107 109 110 111 113 115

Everything wrapped into an adverb that can be applied f:

  sel=.1 : '#~&, [ u (#:i.)@$`
  f sel m
100 101 103 105 107 109 110 111 113 115
like image 36
xash Avatar answered Aug 03 '26 02:08

xash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!