MBaas
MBaas

Reputation: 7538

How to index a mat with 2 cols from another Mat...?

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 ;-)

Upvotes: 2

Views: 56

Answers (2)

Lobachevsky
Lobachevsky

Reputation: 1252

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.

Upvotes: 0

MBaas
MBaas

Reputation: 7538

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? ;-)

Upvotes: 0

Related Questions