I have a namelist (text-matrix) nl43
and multiple indexes into it (in gr43
) and would like to assign the elements index by the 4th and 5th columnn of gr43
to 2 variables, A
and B
.
When accessing a single column, this would be nl43[gr43[;Column];]
, but my fingers just refused to copy & paste that statement to do the 2nd assignment, because my instinct suggested that there must be an easier way ;-)

- 7,248
- 6
- 44
- 61
2 Answers
Ok, so I finally found (A B)←⊂[1 3]nl43[gr43[;4 5;]
and am sadly disappointed by myself, as it never occured to me to re-think this bit before. Now that I answered that question myself, I assume there's not much room for refinement???
Hmm, there is a nested way to do this: (A B)←(⊂nl43){⍺[gr43[;⍵];]}¨4 5
I hesitated to even look at it, because it felt too "clumsy". But performancewise it is a clear winner: .234 secs vs. .64 !!
Comments? ;-)

- 7,248
- 6
- 44
- 61
-
I originally wanted to post this as a real question (after that suggestion from my instinct...). But then I thought some RTFM would help and it did. However, the process might be interesting, and since self-answers are excplicitely encouraged, I thought it might be a nice way to get some action for the APL-tag :-) – MBaas Jan 23 '15 at 11:28
As an analogue to something like
'abcdefghijklmnopqrstuvwxyz'[3 3 reshape 3 1 20 18 1 20 6 1 20] // []A instead of abcde... in Dyalog
cat
rat
fat
// result is a matrix
I would intuitively expect a nested argument to indexing to also work.
'abcdefghijklmnopqrstuvwxyz'[(3 1 20) (18 1 20) (6 1 20)]
cat rat fat
// result is a vector of vectors
Alas, this is not, or not yet, been implemented. I have used a similar dfn approach to indexing in the past but never on anything but a vector. Interesting how this kind of extended indexing could work on matrices and higher-dimensional arrays.

- 1,222
- 9
- 17
-
That could be done with squad-indexing (introduced in Dyalog 14, IIRC): `(⊂3 1 20)(⊂18 1 20)(⊂6 1 20)⌷¨⊂'abcdefghijklmnopqrstuvwxyz'` – MBaas Jan 27 '15 at 07:57
-
Would there be a nice Squad-indexing solution to the Sudoku problem where you have a 9 x 9 matrix and want to split it into a 3 x 3 matrix of 3 x 3 matrices? i.e. `SudokuMat[ (1 2 3) (4 5 6) (7 8 9) ; (1 2 3) (4 5 6) (7 8 9) ]` – Lobachevsky Jan 28 '15 at 09:04
-
Hmm, "nice squad" is a challenging one, I have no idea atm, I'm sorry. I'd do `↑(⊂1 0 0 1 0 0 1 0 0)⊂[1]¨1 0 0 1 0 0 1 0 0⊂SudokuMat` `⍝ ⎕ML=1` – MBaas Jan 28 '15 at 13:53