Search code examples
kdb+q-langk

Adding a row to a matrix in Q?


I currently have a 3 by 3 matrix "m":

1 2 3
4 5 6
7 8 9

I would like to add a row to matrix 'm' to have a resultant matrix of:

1 2 3
4 5 6
7 8 9
10 11 12

Solution

  • A matrix in q is just a list of lists where inner lists represent rows.

    m: ((1 2 3);(4 5 6);(7 8 9))
    

    In order to add one more row all you have to do is add one more inner list to it:

    m: m,enlist 10 11 12
    

    enlist is important here, without it you'll end up with this:

    q)((1 2 3);(4 5 6);(7 8 9)),10 11 12
    1 2 3
    4 5 6
    7 8 9
    10
    11
    12