Apply the verb to the sub-matrix, in place?

In J, I can update a subset of an array in place according to some predicate, for example:

    y (k}~) |. y {~ k =. I. '123' e.~ y =. '[1.2.3]'
[3.2.1]

I understand that I can name a union here, but is there a more elegant way to do this in the first place? 123is just an example. What I want to do, in particular, is:

  • get array of indices into array ( k)
  • retrieve array elements at these indices into a new array
  • converts this array to a new array with the same type and shape
  • return the values ​​of the new array to the slots.

In addition, it is important that the verb work with the array as a whole, because basically I want to rearrange and convert the subarrays into place. (Therefore, |.in the example.)

Is there an easier way to do this?

+4
2

- :

   tweak  =: (@:{) (`[) (`]) }
   twist  =: |. tweak
   '123' (I.@:e.~ twist ]) '[1.2.3]'
[3.2.1]

tweak } :

  • x y {.
  • [sub], @{. {, @: ; twist |. (reverse).
  • [sub] -array y ( ]) x ( [).

:

  • Sub-array J, . : . () , ad-hoc ( ).
  • " " , ( ), , . , ( / ).
  • , : : e., , , |. . , (., , y=.'[1.1.1]').
+5

Amend . , :

v1 =: [: I. e.~        NB. indeces
v0 =: [: |. e.~ # ]   NB. transform the subarray
v2 =: ]

'123' (v0`v1`v2) } '[1.2.3]'
[3.2.1]
+2

Source: https://habr.com/ru/post/1541690/


All Articles