I would like to sort numbers within each row of a matrix, for example,
3 3 rho 2 3 1 4 5 7 1 9 8
should become
3 3 rho 1 2 3 4 5 7 1 8 9
.
I am new to Dyalog APL, and know
how to generate, via grade
the matrix
3 3 rho 3 2 1 1 2 3 1 3 2
from the initial matrix.
There is actually no sort primitive in APL (allegedly, Dyalog APL 19 is supposed to get one). Until then, you would use one of the monadic Grade functions (Grade Up
⍋
in this case), which returns an array of its argument's indices (starting at⎕IO
) permuted such that they can be used as ordered references back into the original array. Provide them (enclosed, and in a fork) to the dyadic Index function⌷
to reference the items in the original array:⊂⍤⍒⌷⊢
. The enclosing (with the monadic Enclose function⊂
) is necessary because when using the Index function⌷
, references to multiple items conveyed as a plain array of indices would instead be interpreted as a single reference into a multi-dimensional array along a number of axes.Use the dyadic Rank operator
⍤
to apply the monadic sort function defined above successively to the first-level (1
) sub-arrays of the matrix:⍤1