indexingapl

Indexing with nested vectors in APL


I have a vector of vectors that contain some indices, and a character vector which I want to use them on.

A←(1 2 3)(3 2 1)
B←'ABC'

I have tried:

      B[A]
RANK ERROR
      B[A]
       ∧
      A⌷B
LENGTH ERROR
      A⌷B
       ∧

and

      A⌷B
LENGTH ERROR
      A⌷¨B
       ∧

I would like

┌→────────────┐
│ ┌→──┐ ┌→──┐ │
│ │ABC│ │CBA│ │
│ └───┘ └───┘ │
└∊────────────┘

to be returned, but if i need to find another way, let me know.


Solution

  • The index function is a bit odd. To select multiple major cells from an array, you need to enclose the array of indices:

          (⊂3 2 1)⌷'ABC'
    CBA
    

    In order to use each of two vectors of indices, the array you're selecting from needs to be distributed among the two. You can use APL's scalar extension for this, but then the array you're selecting from needs to be packaged as a scalar:

          (⊂1 2 3)(⊂3 2 1)⌷¨⊂'ABC'
    ┌→────────────┐
    │ ┌→──┐ ┌→──┐ │
    │ │ABC│ │CBA│ │
    │ └───┘ └───┘ │
    └∊────────────┘
    

    So to use your variables:

          A←(1 2 3)(3 2 1)
          B←'ABC'
          (⊂¨A)⌷¨⊂B
    ┌→────────────┐
    │ ┌→──┐ ┌→──┐ │
    │ │ABC│ │CBA│ │
    │ └───┘ └───┘ │
    └∊────────────┘