# Stan function for converting a (wide) array to (long) matrix

**URL:** <https://discourse.mc-stan.org/t/stan-function-for-converting-a-wide-array-to-long-matrix/4972>\
**Category:** General\
**Created:** [July 25, 2018, 6:09pm UTC](https://discourse.mc-stan.org/t/stan-function-for-converting-a-wide-array-to-long-matrix/4972 "2018-07-25T18:09:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![devincaughey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/devincaughey/32/11466_2.png) [@devincaughey](https://discourse.mc-stan.org/u/devincaughey)\
**Post date:** [July 25, 2018, 6:09pm UTC](https://discourse.mc-stan.org/t/stan-function-for-converting-a-wide-array-to-long-matrix/4972/1 "2018-07-25T18:09:36Z")

</div>

I am curious whether anyone has written a Stan function that converts an array of arbitrary dimensions to a “long” matrix whose first D columns indicate the D indices of the array and whose D+1th column indicates the value in each cell of the array. Figure 16.1 on p. 230 of the Stan manual (version 2.17.0) illustrates the conversion I am thinking of (though it also drops missing elements, which is not essential for my purposes). Obviously this conversion is easy to do in R with a function like reshape2::melt, but ideally I would also be able to do it within Stan as well. My sense from reading Stan forums is that there has been some demand for such a function in the past, but I wasn’t able to locate any workable solutions. Any help would be appreciated. Thanks!

Devin

---

<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:** [July 25, 2018, 9:22pm UTC](https://discourse.mc-stan.org/t/stan-function-for-converting-a-wide-array-to-long-matrix/4972/2 "2018-07-25T21:22:48Z")

</div>

Not that I know of. We tend to do it on the outside as part of data prep.

There’s no way to write that kind of polymorphic function actually in the Stan user-defined function syntax. I t could be done in C++ and either hacked into the language as a specialized expression or defined for a finite set of instantiations.

What you are probably going to want out is not quite a long matrix, but an integer array of the indices and a real array or vector of values. And there’s no way to return multiple types like this until we have tuples.  
What you’d need, say for a 3D array is:

```
data {
  ...
  real x[I, J, K];
}
transformed data {
  int N = I * J * K;
  int x_idx[N, 3]
  vector x_val[N];
  {
    int pos = 1;
    for (i in 1:I) {
      for (j in 1:J) {
        for (k in 1:K) {
          x_idx[pos] = { i, j, k };
          x_val[pos] = x[i, j, k];
          pos += 1;
        }
      }    
    }
  }
}

```

For neatness, I made `pos` a local variable. That’s not necessary for efficiency.

---

<div class="post-metadata">

**Author:** ![devincaughey](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/devincaughey/32/11466_2.png) [@devincaughey](https://discourse.mc-stan.org/u/devincaughey)\
**Post date:** [July 26, 2018, 12:29am UTC](https://discourse.mc-stan.org/t/stan-function-for-converting-a-wide-array-to-long-matrix/4972/3 "2018-07-26T00:29:47Z")

</div>

Thanks, Bob. As always, I appreciate your swift and helpful reply. I had something like this in mind but couldn’t quite figure out how to implement it. I’ll work on trying to generalize it and email back if I come up with something useful. Thanks again!

Devin

---

<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:** [July 28, 2018, 2:14pm UTC](https://discourse.mc-stan.org/t/stan-function-for-converting-a-wide-array-to-long-matrix/4972/4 "2018-07-28T14:14:25Z")

</div>

I really don’t think there’s an easy way to generalize given the way Stan works (the problem is setting `x_val` and increment `pos` in a function).

It would be easy enough to write a general C++ template metaprogram to do this—it’s what a lot of our I/O and vectorized operations do under the hood.
