KDB indexing a matrix

Viewed 83

I have two matrices:

a: (0.5 0.5; 0.3 0.7)
idx: ((0 0);(0 1);(1 1))

I want to use the idx as i and j pairs to get the values out of a. In longform, this would be:

a[0;0]
a[0;1]
a[1;1]

Currently, when I try to index it as a idx, it behaves as if I've entered a[0 0] etc.

What am I doing wrong?

2 Answers

You want to use . index with each right /: here:

a ./:idx

Apply Each Right (@kylebonnes) is the correct answer to OP. You might prefer an alternative approach to scattered indexing if it suited you better to represent a as a vector.

q)M:2 2#V:5 5 3 7%10  / matrix; vector
q)r:0 0 1;c:0 1 1     / rows; columns

q)M ./:r,'c
0.5 0.5 0.7

q)V 0 2 sv(r;c)       / index V as if it were M
0.5 0.5 0.7

Here the left argument sv specifies a (variable) arithmetic base. For rows r and columns c you can use (0,N)sv(r;c) as indexes into a razed N-column matrix.

q)show A:4 5#B:"the  quickbrownfox  "
"the  "
"quick"
"brown"
"fox  "
q)rc:6 2#2 0 2 1 1 1 2 4 1 3 0 1
q)A ./: rc
"brunch"
q)B 0 5 sv flip rc
"brunch"
Related