# Need to create lists out each row of a matrix

**URL:** https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [March 31, 2016, 12:35pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924 "2016-03-31T12:35:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Krishna1](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krishna1](https://forum.kx.com/u/Krishna1)
#### Post date: [March 31, 2016, 12:35pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924/1 "2016-03-31T12:35:00Z")

</div>

Hello,

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

eg:&nbsp;

matrix :&nbsp;

1 2&nbsp;

2 3

4 5

4 6

result-\>&nbsp;

((1;2);(2;3);(4;5);(4;6))

Thanks,&nbsp;

Kumar

---

<div class="post-metadata">

### Author: ![thomas\_smyth](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@thomas\_smyth](https://forum.kx.com/u/thomas_smyth)
#### Post date: [March 31, 2016, 12:49pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924/2 "2016-03-31T12:49:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Krishna1](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krishna1](https://forum.kx.com/u/Krishna1)
#### Post date: [March 31, 2016, 1:04pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924/3 "2016-03-31T13:04:00Z")

</div>

Thanks, Thomas.&nbsp;  
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.&nbsp;

In my case, when I do b[a[0]], I get&nbsp;

6 &nbsp;7 &nbsp;8 &nbsp;9 &nbsp;10 11

12 13 14 15 16 17,&nbsp;

when , actually I want b[1;2] -\> 8.&nbsp;

Regards,&nbsp;

Krishna

---

<div class="post-metadata">

### Author: ![Krishna1](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krishna1](https://forum.kx.com/u/Krishna1)
#### Post date: [March 31, 2016, 1:20pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924/4 "2016-03-31T13:20:00Z")

</div>

Little roundabout code:  
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; -\>&nbsp;

z:(rows,cols]#z;

My two pence.&nbsp;

Kumar

---

<div class="post-metadata">

### Author: ![thomas\_smyth](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@thomas\_smyth](https://forum.kx.com/u/thomas_smyth)
#### Post date: [March 31, 2016, 1:39pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924/5 "2016-03-31T13:39:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Krishna1](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krishna1](https://forum.kx.com/u/Krishna1)
#### Post date: [March 31, 2016, 1:55pm UTC](https://forum.kx.com/t/need-to-create-lists-out-each-row-of-a-matrix/10924/6 "2016-03-31T13:55:00Z")

</div>

Much appreciated, Thomas.&nbsp;  
Regards,&nbsp;

Kumar
