3

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.

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Jonathan
  • 109
  • 6

3 Answers3

3

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]
emc211
  • 1,369
  • 7
  • 14
1

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:

MdSalih
  • 1,978
  • 10
  • 16
  • Marvellous, thank you. I'm not too familiar with dot amend. I'll do a bit of research. Thanks very much! – Jonathan Jun 16 '17 at 09:15
0

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
nyi
  • 3,123
  • 4
  • 22
  • 45