# How to declare a integer-valued matrix?

**URL:** <https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845>\
**Category:** Modeling\
**Tags:** matrix\
**Created:** [October 23, 2020, 12:42pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845 "2020-10-23T12:42:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobinz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tobinz/32/9669_2.png) [@tobinz](https://discourse.mc-stan.org/u/tobinz)\
**Post date:** [October 23, 2020, 12:42pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/1 "2020-10-23T12:42:01Z")

</div>

My data is a matrix `X` with the dimension `100 \times 3`, whose each element follows a Poisson distribution. Therefore, each element of it must be nonnegative. I wonder how I can declare such matrix in the data block? because an error message reminds me of defining `X` correctly.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 23, 2020, 1:16pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/2 "2020-10-23T13:16:33Z")

</div>

You can define a 2D array of non-negative integers like:

```
int<lower=0> X[100, 3];

```

Does this solve your error? If not what was the message?

---

<div class="post-metadata">

**Author:** ![tobinz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tobinz/32/9669_2.png) [@tobinz](https://discourse.mc-stan.org/u/tobinz)\
**Post date:** [October 23, 2020, 1:26pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/3 "2020-10-23T13:26:06Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![Xavier\_La\_Rochelle](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/xavier_la_rochelle/32/16081_2.png) [@Xavier\_La\_Rochelle](https://discourse.mc-stan.org/u/Xavier_La_Rochelle)\
**Post date:** [May 27, 2024, 4:42pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/4 "2024-05-27T16:42:14Z")

</div>

Hello,

As of Stan 2.26, declaring arrays with this syntax does not work (see here). Since matrices and vectors are by default declared as reals, how can we now define vectors and matrices of integers? My first guess was :

`matrix[100, 3] int<lower=0> X; `

However, I’m getting this error messages from the parser :

`"[" expression "," expression "]" expected for matrix sizes.`

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [May 27, 2024, 4:51pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/5 "2024-05-27T16:51:42Z")

</div>

It would be `array[100,3] int<lower=0> X;`

---

<div class="post-metadata">

**Author:** ![Xavier\_La\_Rochelle](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/xavier_la_rochelle/32/16081_2.png) [@Xavier\_La\_Rochelle](https://discourse.mc-stan.org/u/Xavier_La_Rochelle)\
**Post date:** [May 27, 2024, 5:17pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/6 "2024-05-27T17:17:40Z")

</div>

This works but I’m wondering if declaring X as an array instead of a matrix could be inconvenient in some way. After reading [this thread](https://discourse.mc-stan.org/t/new-array-syntax-might-mean-stan-language-3-0/17448/13) on the topic, I believe using basic types (matrix, vector, row\_vector, etc.) is generally preferable since you can do matrix arithmetic and linear algebra with them. Is it not possible at all?

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [May 27, 2024, 5:53pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/7 "2024-05-27T17:53:28Z")

</div>

No, there is no integer matrix/vector type in Stan

---

<div class="post-metadata">

**Author:** ![andrewgelman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrewgelman/32/1577_2.png) [@andrewgelman](https://discourse.mc-stan.org/u/andrewgelman)\
**Post date:** [May 27, 2024, 8:43pm UTC](https://discourse.mc-stan.org/t/how-to-declare-a-integer-valued-matrix/18845/8 "2024-05-27T20:43:27Z")

</div>

You can define the integer array and then convert from array to matrix (which will automatically be real) if you want to use it in matrix calculations. You would use some function called to\_matrix or something like that.
