# How to convert a matrix to an array of vectors?

**URL:** <https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315>\
**Category:** Modeling\
**Created:** [October 25, 2017, 10:00pm UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315 "2017-10-25T22:00:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![flaxter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/flaxter/32/412_2.png) [@flaxter](https://discourse.mc-stan.org/u/flaxter)\
**Post date:** [October 25, 2017, 10:00pm UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/1 "2017-10-25T22:00:53Z")

</div>

I’ve got a matrix in my model block:

```
matrix[3,5] x;

```

Which I need to pass to `cov_exp_quad`, which expects an array of vectors, i.e.:

```
vector[5] x[3];

```

Is there a simple way to convert it, something like to\_vector? If x was in the data block it would be fine, but x results from some linear algebra operations so I think it has to be a matrix.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [October 25, 2017, 10:05pm UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/2 "2017-10-25T22:05:54Z")

</div>

You have to loop over the rows.

---

<div class="post-metadata">

**Author:** ![phildias](https://avatars.discourse-cdn.com/v4/letter/p/a6a055/32.png) [@phildias](https://discourse.mc-stan.org/u/phildias)\
**Post date:** [October 26, 2017, 2:21am UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/3 "2017-10-26T02:21:42Z")

</div>

This is pretty similar to what I was asking a few days ago [here](http://discourse.mc-stan.org/t/speeding-up-a-multi-normal-model/2289/4).

I found that the best way, as @bgoodri suggested, is to use a for-loop to convert one object into another.

So use the matrix variable to perform your linear algebra operations, but then create a new variable (say `x_temp`) and add a for loop to copy each row of `x` to `x_temp`.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [November 2, 2017, 10:08pm UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/4 "2017-11-02T22:08:09Z")

</div>

We need to have built-ins for this. I created a feature request:

[https://github.com/stan-dev/math/issues/669](https://github.com/stan-dev/math/issues/669)

---

<div class="post-metadata">

**Author:** ![cwhittaker1000](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cwhittaker1000/32/13135_2.png) [@cwhittaker1000](https://discourse.mc-stan.org/u/cwhittaker1000)\
**Post date:** [November 19, 2021, 11:11am UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/5 "2021-11-19T11:11:03Z")

</div>

Related to this, apologies if obvious but wasn’t clear to me from above or the Stan user manual.

Is the following correct?

`vector[N] x[K]` specifies an object `x` which is an array (length `K` ) of vectors (length `N`). And to access the first vector (length `N`) in the array, you can do `x[1,]`

---

<div class="post-metadata">

**Author:** ![spinkney](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@spinkney](https://discourse.mc-stan.org/u/spinkney)\
**Post date:** [November 19, 2021, 11:27am UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/6 "2021-11-19T11:27:33Z")

</div>

Yes that is correct. You may be viewing an older version of the manual. The updated version uses the `array` syntax and discusses this [18.4 Multiple indexes with vectors and matrices | Stan User’s Guide](https://mc-stan.org/docs/2_28/stan-users-guide/multiple-indexes-with-vectors-and-matrices.html).

---

<div class="post-metadata">

**Author:** ![cwhittaker1000](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cwhittaker1000/32/13135_2.png) [@cwhittaker1000](https://discourse.mc-stan.org/u/cwhittaker1000)\
**Post date:** [November 19, 2021, 11:36am UTC](https://discourse.mc-stan.org/t/how-to-convert-a-matrix-to-an-array-of-vectors/2315/7 "2021-11-19T11:36:12Z")

</div>

Thank you very much @spinkney!
