Jonathan
Jonathan

Reputation: 109

Take a column of a matrix and make it a row in kdb

Consider the matrix:

1 2 3
4 5 6
7 8 9 

I'd like to take the middle column, assign it to a variable, and replace the middle row with it, giving me

1 2 3
2 5 8
7 8 9 

I'm extracting the middle column using

a:m[;enlist1]

which returns

2
5
8

How do I replace the middle row with a? Is a flip necessary?

Thanks.

Upvotes: 3

Views: 1212

Answers (3)

nyi
nyi

Reputation: 3229

Making it slightly more generic where you can define the operation, row, and column

q)m:3 cut 1+til 9
1 2 3
4 5 6
7 8 9

Assigning the middle column to middle row :

q){[ m;o;i1;i2] .[m;enlist i1;o; flip[m] i2 ] }[m;:;1;1]
1 2 3
2 5 8
7 8 9

Adding the middle column to middle row by passing o as +

q){[ m;o;i1;i2] .[m;enlist i1;o; flip[m] i2 ] }[m;+;1;1]
1 2  3
6 10 14
7 8  9

Upvotes: 0

emc211
emc211

Reputation: 1379

If you want to update the matrix in place you can use

q)show m:(3;3)#1+til 10
1 2 3
4 5 6
7 8 9
q)a:m[;1]
q)m[1]:a
q)show m
1 2 3
2 5 8
7 8 9
q)

cutting out "a" all you need is:

m[1]:m[;1]

Upvotes: 3

MdSalih
MdSalih

Reputation: 1996

You can use dot amend -

q)show m:(3;3)#1+til 10
    1 2 3
    4 5 6
    7 8 9
q)show a:m[;1]
    2 5 8
q).[m;(1;::);:;a]
    1 2 3
    2 5 8
    7 8 9

Can see documentation here:

Upvotes: 1

Related Questions