cancel
Showing results for
Did you mean:

## Need to create lists out each row of a matrix

New Contributor
Hello,

How may I create a list of lists, each inner list being a row of a matrix?

eg:
matrix :
1 2
2 3
4 5
4 6

result->
((1;2);(2;3);(4;5);(4;6))

Thanks,
Kumar
5 REPLIES 5
New Contributor
Hi Krishna,

Not exactly sure what you mean here, the example you have given is a list of lists (i.e. a matrix):
q)((1;2);(2;3);(4;5);(4;6))
1 2
2 3
4 5
4 6

If you wish to create a list of lists/matrix from a single list consider using # with the dimensions of the matrix:
q)(4 2)#til 8
0 1
2 3
4 5
6 7

For an unknown length you may use:
q)(0N 2)#til 8
0 1
2 3
4 5
6 7

Which gives an equivalent result to using cut:
q)2 cut til 8
0 1
2 3
4 5
6 7

Regards,
Thomas Smyth
AquaQ Analytics

New Contributor
Thanks, Thomas.
Let me clarify:
I want to treat a row of a matrix as an index into another matrix.
q) a:((1;2);(2;3);(4;5);(4;6))
q) b:6 6 # til 36(for example)
I want to use each element of 'a' as a (row;column) pair.
In my case, when I do b[a[0]], I get
6  7  8  9  10 11
12 13 14 15 16 17,
when , actually I want b[1;2] -> 8.

Regards,
Krishna

New Contributor
a:((1;2);(2;3);(3;5);(4;6)) -> matrix containing indices (row;col) pairs.
rows:500
cols: 10
z:(rows*cols) # 0;
z[(cols*a[;0])+a[;1]]:1; ->
z:(rows,cols]#z;

My two pence.
Kumar
New Contributor
Hi,

You could use either of the following:
q)b ./: a
8 15 29 0N
q).[b;] each a
8 15 29 0N

Regards,
Thomas Smyth
AquaQ Analytics
New Contributor
Much appreciated, Thomas.
Regards,
Kumar