rslt ← indx ##.mesh avec                    ⍝ ⍺-mesh of items of 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.

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

Technical note:

Note that in the coding for mesh:

    mesh←{                ⍝ ⍺-mesh 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 mesh as a single derived function:

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

    mesh ddft 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 mesh (1 2 3 4 5)(10 20 30 40 50)        ⍝ enmesh vectors.
10 2 30 40 5

      mats←5 5∘⍴¨⎕a(lcase ⎕a)⎕d                         ⍝ vector of matrices.

      disp mats
┌→────┬─────┬─────┐ 
│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 mesh mats                    ⍝ enmesh matrices.
Ab2De
5Gh8J
k1Mn4
Pq7St
0Vw3Y

      chars                             ⍝ character matrix.
now is
the time

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

⍝ This function swaps line-drawing characters for printer-friendly equivalents:

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

      disp ⍳¨⍳2 2               ⍝ char mat with line 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.

      disp cubes
┌→──────────┬───────────────┬───────────────────┐ 
│ 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 mesh cubes                   ⍝ mesh 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: from enss ddft merge

Back to: contents

Back to: Dyalog APL

Trouble seeing APL font?