rslt ← indx ##.select avec              ⍝ ⍺-selection of items from vector ⍵.

[indx]  is an S-array (an array of shape S), each item of which is an index into
the  vector of S-arrays [avec]. An item of avec may also be a single item array,
in which case it is notionally S-reshaped.

The  resulting  S-array is an item-wise selection of corresponding elements from
the items of avec.

Note  that in origin-0, the left argument may be a boolean array to select items
from a pair of false-true arrays on the right.

Select may often be used as a pure, though expensive, alternative to partial as-
signments ]← and )←.

(
    Bob Bernecky points us to Ken Iverson's "mask":

        "The MASK operation is defined formally as follows:

          c ← /a,u,b/ <==> (~u)/c = (~u)/a,  and  u/c = u/b.

        The vectors c, a, u, and b are clearly of a common dimension and
        c[i]= a[i] or b[i], according as u[i] = 0 or u[i] = 1."

    Ref: Kenneth E. Iverson, "A Programming Language", p21, Wiley, 1962.

    http://www.jsoftware.com/papers/APL.htm
)

Technical note:

Note that in the coding for select:

    select←{              ⍝ ⍺-selection of items of vector ⍵.
        ⍺⊃¨↑,¨/⊂¨¨⍵
    }

⍺  and  ⍵  appear  just once at each end of the selection expression. This means
that, if we bound adjacent _functions_ in the expression using compose operators
we could express select as a single derived function. See →tacit←

    select ← ⊃¨∘↑∘(,¨/)∘(⊂¨¨)   ⍝ derived function for select.

    select dft 1                ⍝ show derived function tree.
              ∘
        ┌─────┴─────┐
        ∘           ¨
    ┌───┴───┐     ┌─┘
    ∘       /     ¨
  ┌─┴─┐   ┌─┘   ┌─┘
  ¨   ↑   ¨     ⊂
┌─┘     ┌─┘
⊃       ,

However, there is not much of a case for doing so. Although the resulting funct-
ion is arguably "cute", it runs no quicker and is harder to maintain.

Examples:

    2 1 2 2 1 select (1 2 3 4 5)(10 20 30 40 50)        ⍝ selection from vectors
10 2 30 40 5

    ⊢ mats ← 5 5∘⍴¨⎕a(lcase ⎕a)⎕d                       ⍝ vector of matrices
┌─────┬─────┬─────┐
│ABCDE│abcde│01234│
│FGHIJ│fghij│56789│
│KLMNO│klmno│01234│
│PQRST│pqrst│56789│
│UVWXY│uvwxy│01234│
└─────┴─────┴─────┘

    ⎕←indx←5 5⍴⍳3                       ⍝ selection matrix.
1 2 3 1 2
3 1 2 3 1
2 3 1 2 3
1 2 3 1 2
3 1 2 3 1

    indx select mats                    ⍝ selection from matrices.
Ab2De
5Gh8J
k1Mn4
Pq7St
0Vw3Y

    chars                               ⍝ character matrix.
now is
the time

    (⎕io+chars=' ')select chars '.'     ⍝ dots for blanks.
now.is..
the.time

⍝ The following function swaps box-drawing characters for the clunky equivalents
⍝ that old-fashioned printers preferred:

    clunk←{                     ⍝ Printer friendly characters.
        fm←'┌┬┐├┼┤└┴┘│─'        ⍝ box drawing characters.
        to←'...|+|''''''|-'     ⍝ equivalent clunky chars.
        (fm⍳⍵)select to,⊂⍵      ⍝ printer-friendly substitution.
    }

    disp ⍳¨⍳2 2                 ⍝ char mat with box-drawing characters
┌─────┬─────────┐
│┌───┐│┌───┬───┐│
││1 1│││1 1│1 2││
│└───┘│└───┴───┘│
├─────┼─────────┤
│┌───┐│┌───┬───┐│
││1 1│││1 1│1 2││
│├───┤│├───┼───┤│
││2 1│││2 1│2 2││
│└───┘│└───┴───┘│
└─────┴─────────┘

    clunk disp ⍳¨⍳2 2           ⍝ clunky equivalents.
.-----.---------.
|.---.|.---.---.|
||1 1|||1 1|1 2||
|'---'|'---'---'|
|-----+---------|
|.---.|.---.---.|
||1 1|||1 1|1 2||
||---|||---+---||
||2 1|||2 1|2 2||
|'---'|'---'---'|
'-----'---------'

    ⊢ cubes ← 1 10 100×⊂2 3 4⍴⍳24       ⍝ vector of higher-rank arrays.
┌───────────┬───────────────┬───────────────────┐
│ 1  2  3  4│ 10  20  30  40│ 100  200  300  400│
│ 5  6  7  8│ 50  60  70  80│ 500  600  700  800│
│ 9 10 11 12│ 90 100 110 120│ 900 1000 1100 1200│
│           │               │                   │
│13 14 15 16│130 140 150 160│1300 1400 1500 1600│
│17 18 19 20│170 180 190 200│1700 1800 1900 2000│
│21 22 23 24│210 220 230 240│2100 2200 2300 2400│
└───────────┴───────────────┴───────────────────┘

      ⎕←indx←2 3 4⍴⍳3                   ⍝ higher-rank index array.
1 2 3 1
2 3 1 2
3 1 2 3

1 2 3 1
2 3 1 2
3 1 2 3

      indx select cubes                 ⍝ selection of higher rank arrays.
   1   20  300    4
  50  600    7   80
 900   10  110 1200

  13  140 1500   16
 170 1800   19  200
2100   22  230 2400

See also: at from dft tacit

Back to: contents

Back to: Workspaces