⍝ Select (1↓⍴⍵)-cells from array ⍵:

    ⎕io←1

    a1 a2 a3 a4← {10⊥¨⍳⌽⍵}¨,\5 4 3 2        ⍝ test arrays.

    ⍕display¨a1 a2 a3 a4
 ┌→────────┐  ┌→─────────────┐  ┌┌→──────────────────┐  ┌┌┌→───────────────────────┐ 
 │1 2 3 4 5│  ↓11 12 13 14 15│  ↓↓111 112 113 114 115│  ↓↓↓1111 1112 1113 1114 1115│ 
 └~────────┘  │21 22 23 24 25│  ││121 122 123 124 125│  │││1121 1122 1123 1124 1125│ 
              │31 32 33 34 35│  ││131 132 133 134 135│  │││1131 1132 1133 1134 1135│ 
              │41 42 43 44 45│  ││141 142 143 144 145│  │││1141 1142 1143 1144 1145│ 
              └~─────────────┘  ││                   │  │││                        │ 
                                ││211 212 213 214 215│  │││1211 1212 1213 1214 1215│ 
                                ││221 222 223 224 225│  │││1221 1222 1223 1224 1225│ 
                                ││231 232 233 234 235│  │││1231 1232 1233 1234 1235│ 
                                ││241 242 243 244 245│  │││1241 1242 1243 1244 1245│ 
                                ││                   │  │││                        │ 
                                ││311 312 313 314 315│  │││1311 1312 1313 1314 1315│ 
                                ││321 322 323 324 325│  │││1321 1322 1323 1324 1325│ 
                                ││331 332 333 334 335│  │││1331 1332 1333 1334 1335│ 
                                ││341 342 343 344 345│  │││1341 1342 1343 1344 1345│ 
                                └└~──────────────────┘  │││                        │ 
                                                        │││                        │ 
                                                        │││2111 2112 2113 2114 2115│ 
                                                        │││2121 2122 2123 2124 2125│ 
                                                        │││2131 2132 2133 2134 2135│ 
                                                        │││2141 2142 2143 2144 2145│ 
                                                        │││                        │ 
                                                        │││2211 2212 2213 2214 2215│ 
                                                        │││2221 2222 2223 2224 2225│ 
                                                        │││2231 2232 2233 2234 2235│ 
                                                        │││2241 2242 2243 2244 2245│ 
                                                        │││                        │ 
                                                        │││2311 2312 2313 2314 2315│ 
                                                        │││2321 2322 2323 2324 2325│ 
                                                        │││2331 2332 2333 2334 2335│ 
                                                        │││2341 2342 2343 2344 2345│ 
                                                        └└└~───────────────────────┘ 

      _←,⊂⍬                 ⍝ special value => all indices.

      (⊂2 1) from a1        ⍝ items from vector.
2 1
      (2 1)_ from a2        ⍝ rows from matrix.
21 22 23 24 25
11 12 13 14 15

      _(2 1)from a2         ⍝ cols from matrix.
12 11
22 21
32 31
42 41

      (2 1)_ _ from a3      ⍝ 1-planes from cuboid.
211 212 213 214 215
221 222 223 224 225
231 232 233 234 235
241 242 243 244 245
                   
111 112 113 114 115
121 122 123 124 125
131 132 133 134 135
141 142 143 144 145

      _(2 1)_ from a3       ⍝ 2-planes from cuboid.
121 122 123 124 125
111 112 113 114 115
                   
221 222 223 224 225
211 212 213 214 215
                   
321 322 323 324 325
311 312 313 314 315

      _ _(2 1)from a3       ⍝ 3-planes from cuboid.
112 111
122 121
132 131
142 141
       
212 211
222 221
232 231
242 241
       
312 311
322 321
332 331
342 341

      ⍴ (6 7⍴1)_ _ from a3  ⍝ high-rank index array.
6 7 4 5
      ⍴ _(6 7⍴1)_  from a3
3 6 7 5
      ⍴ _ _(6 7⍴1) from a3
3 4 6 7

      ⍴ (6 7⍴1)_ _ _ from a4
6 7 3 4 5
      ⍴ _(6 7⍴1)_ _  from a4
2 6 7 4 5
      ⍴ _ _(6 7⍴1)_  from a4
2 3 6 7 5
      ⍴ _ _ _(6 7⍴1) from a4
2 3 4 6 7


      ⍴⍴1 from a1           ⍝ scalar index.
0
      ⍴⍴1 _ from a2
1
      ⍴⍴1 _ _ from a3
2
      ⍴⍴1 _ _ _ from a4
3

      (2 1)(2 1)(2 1)(2 1) from a4
2222 2221
2212 2211
         
2122 2121
2112 2111
         
         
1222 1221
1212 1211
         
1122 1121
1112 1111

      _(2 1)(2 1) from a3
122 121
112 111
       
222 221
212 211
       
322 321
312 311

      (2 1)_(2 1) from a3
212 211
222 221
232 231
242 241
       
112 111
122 121
132 131
142 141

      (2 1)(2 1)_ from a3
221 222 223 224 225
211 212 213 214 215
                   
121 122 123 124 125
111 112 113 114 115

    ix1←2 2⍴2 1                         ⍝ first axis index

    a3[ix1;;] ≡ ix1 _ _ from a3         ⍝ [] vs from  indexing.
1

    ix3←2 3 4⍴2 4 5                     ⍝ third axis index.

    a3[ix1;;ix3] ≡ ix1 _ ix3 from a3    ⍝ [] vs from  indexing.
1

    ⍴(,1)(,2)(,3)  from a3              ⍝ rank-3 (1×1×1) result.
1 1 1

    ⍴⍴(,1)(,2)(,3)  from a3             ⍝ rank-3 result.
3
    ⍴⍴(,1)( 2)(,3)  from a3             ⍝ rank-2 result.
2
    ⍴⍴( 1)( 2)(,3)  from a3             ⍝ rank-1 result.
1
    ⍴⍴( 1)( 2)( 3)  from a3             ⍝ rank-0 result.
0

    ⍴(↑⍴⍨¨/3/⊂⍳4)from a4                ⍝ show off high-rank indexing.
1 2 2 3 3 3 4 4 4 4

    ⍴(↑⍴⍨¨/1 2/{⌽⍵}\2/⊂⍳4)from a4
4 4 4 4 3 3 3 2 2 1

    (0⍴⊂_)from 99                       ⍝ null index into scalar.
99

    (⊂2 2⍴2 1 1)from'hello' 'world'     ⍝ works for nested arg.
┌─────┬─────┐
│world│hello│
├─────┼─────┤
│hello│world│
└─────┴─────┘

⍝∇ from display

Back to: code

Back to: Workspaces