# How to pass an array of integer arrays of different lengths into Stan?

**URL:** <https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342>\
**Category:** Modeling\
**Created:** [April 3, 2019, 12:53am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342 "2019-04-03T00:53:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![willte](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/willte/32/3120_2.png) [@willte](https://discourse.mc-stan.org/u/willte)\
**Post date:** [April 3, 2019, 12:53am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342/1 "2019-04-03T00:53:46Z")

</div>

I’m facing the problem described in this post:

[https://groups.google.com/forum/#!topic/stan-users/oX6Uq100\_Y0](https://groups.google.com/forum/#!topic/stan-users/oX6Uq100_Y0)

Where the most appropriate solution (given I can’t order by group) seems to be that proposed by @jonah to pass an array an integer arrays into Stan containing the indexes for each group.

I’m struggling to determine how to pass this array of arrays into Stan though, when the integer array of each group is a different length? Given I believe I have to define consistent dimensions of the array in the data block of Stan. Perhaps this is not even yet possible in Stan?

---

<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:** [April 3, 2019, 2:19am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342/2 "2019-04-03T02:19:51Z")

</div>

This came up (among other places) at

> <https://stackoverflow.com/questions/55364960/how-to-pass-a-list-of-vectors-with-various-length-and-list-of-matrices-with-vari/55368618#55368618>

  
It is basically the same considerations with integer arrays.

---

<div class="post-metadata">

**Author:** ![willte](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/willte/32/3120_2.png) [@willte](https://discourse.mc-stan.org/u/willte)\
**Post date:** [April 3, 2019, 3:25am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342/3 "2019-04-03T03:25:33Z")

</div>

Thanks @bgoodri. Is it ok to put `inf` values in integer arrays? (as the padding option).

Would this method work as a simplified example of getting the `inf` values out of the vector before using the integer values to index a vector?

```stan
data {
   int N; //num observations
   int Ind[N, 5]; //Array of N integer arrays of length 5 padded with Inf (at the end when total non-inf values < 5)
   vector[20] Vals; //vector to subset 
}
model {
   for (i in 1:N) {
      int IndCurrRow[5] = Ind[i,1:5]; //select integer array for current row
      int NumNonInfs = sum(!is.inf(IndCurrRow)); //count number of non-infinity values in array
      int IndNoInf[NumNonInfs] = IndCurrRow[1:NumNonInfs]; //create integer array of length equal to non-Inf values in current integer array
      vector[NumNonInfs] SubsetVals = Vals[IndNoInf]; //subset Vals with indexes from IndNoInf
   }
}
```

---

<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:** [April 3, 2019, 3:26am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342/4 "2019-04-03T03:26:44Z")

</div>

Integers are limited to \mp 2^{31} \pm 1 so I would use the most negative one as a padding value.

---

<div class="post-metadata">

**Author:** ![willte](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/willte/32/3120_2.png) [@willte](https://discourse.mc-stan.org/u/willte)\
**Post date:** [April 3, 2019, 3:33am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342/5 "2019-04-03T03:33:32Z")

</div>

Thanks @bgoodri so adjusting the code above to now use -1 as the padding value, I now get (and hopefully you think this looks sensible):

```stan
data {
   int N; //num observations
   int Ind[N, 5]; //Array of N integer arrays of length 5 padded with -1 (at the end when total non-Neg values < 5)
   vector[20] Vals; //vector to subset 
}
model {
   for (i in 1:N) {
      int IndCurrRow[5] = Ind[i,1:5]; //select integer array for current row
      int NumPositive = sum(IndCurrRow >= 0); //count number of positive values in array
      int IndPositive[NumPositive] = IndCurrRow[1:NumPositive]; //create integer array of length equal total positive values in current integer array
      vector[NumPositive] SubsetVals = Vals[IndPositive]; //subset Vals with indexes from IndPositive
   }
} 

```

---

<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:** [April 3, 2019, 3:36am UTC](https://discourse.mc-stan.org/t/how-to-pass-an-array-of-integer-arrays-of-different-lengths-into-stan/8342/6 "2019-04-03T03:36:02Z")

</div>

I am not sure `sum(IndCurrRow >= 0)` is legal, but that ought to be something you can tabulate in the transformed data block.
