Reputation: 105
(Dyalog) APL learner question
If I have a matrix Y:
Y
4 9 2
3 5 7
8 1 6
I can get two of its members like this:
Y[(1 1) (2 2)]
4 5
I can use the same technique using dfn syntax:
{⍵[(1 1) (2 2)]}Y
4 5
I, however, can't work out how to do the equivalent in a tacit function. In particular it seems that bracket indexing doesn't work in a tacit function, and I can't find a way of using squad indexing with list of indexes.
Is there a way of doing this, or is this a limitation of tacit functions?
Note that in my real example the list of indexes is generated, so I can't simply do (((1 1)⌷⊢),(2 2)⌷⊢)Y
or anything similar.
Upvotes: 2
Views: 395
Reputation: 981
The first thing one might try is
Y ← 3 3⍴⍳9
Y
1 2 3
4 5 6
7 8 9
Y[(1 1)(2 2)]
1 5
1 1⌷Y
1
(1 1)(2 2)⌷Y
2 2
2 2
But we see that (1 1)(2 2)⌷Y
doesn't work. What is happening is that ⌷
looks at the vectors on its left and builds all combinations of indices, which just builds a 2 by 2 matrix of 2s, as (1 1)(2 2)
is interpreted as the indices (1 2)
, (1, 2)
, (1, 2)
, and then (1, 2)
again.
It might be easier to see it like this:
(1 2)3⌷Y
3 6
(1 2)3⌷
means "from the first and second rows, give me the element in the 3rd column".
Therefore, if you want to give the indices like that, you are likely to need to use the each operator ¨
with ⌷
:
(1 1)(2 2)⌷¨⊂Y
1 5
If you really want that tacitly, then you can use
I ← ⌷¨∘⊂
As other answer(s) have shown, there are more alternatives to indexing. I can also recommend the following webinar on indexing: https://dyalog.tv/Webinar/?v=AgYDvSF2FfU .
Take your time to go through the alternatives in the video, APL isn't like Python: in APL, there's generally more than one obvious way to do it :)
Upvotes: 3