1

In APL, matrices and vectors are used to hold data. I was wondering if there was a way to search within a matrix for a given value, and have that values index returned. For example, say I have the following 2-dimensional matrices:

VALUES ← 1 2 3 4 5 6 7 8 9 10 11... all the way up to 36

KINDS ← 0 0 0 2 0 0 0 3 0 ... filled with 0's the rest of the way to 36 length.

If I laminated these two matrices with

kinds,[.5] values

so that they are laminated one on top of the other

1 2 3 4 5 6 7 8 9 10...
0 0 0 2 0 0 0 3 0 ....    

is there a functionally easy way to search for the index of the 2 value in the "second row" of the newly laminated matrix? eg. the column containing

4
2

and return that matrix index?

Adám
  • 6,573
  • 20
  • 37
z.rubi
  • 327
  • 2
  • 15

1 Answers1

2

The value 2 also appears in row 1 of your newly laminated matrix (nlm), and as you stated, you really do not want to search the whole matrix, but only the second row. So, since you're only searching within a given row, getting the column index in that row gives you the complete answer:

    row←2
    ⎕←col←nlm[row;]⍳2
4
    nlm[;col]  ⍝ values in matched column
4 2

Try it online!

MBaas
  • 7,248
  • 6
  • 44
  • 61
  • @z.rubi: can you pls. accept the answer, so that the question is no longer shown as "open" (just tick the check-mark next to the reply). – MBaas Mar 26 '18 at 07:52