# How to declare and use integer matrices

**URL:** <https://discourse.mc-stan.org/t/how-to-declare-and-use-integer-matrices/16263>\
**Category:** Modeling\
**Tags:** matrix\
**Created:** [June 28, 2020, 5:15pm UTC](https://discourse.mc-stan.org/t/how-to-declare-and-use-integer-matrices/16263 "2020-06-28T17:15:05Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [June 29, 2020, 12:37pm UTC](https://discourse.mc-stan.org/t/how-to-declare-and-use-integer-matrices/16263/4 "2020-06-29T12:37:28Z")

</div>

> For the 2nd question how do you get `int k = max(y)` to work? Do I need to do `max(max(y[,1]),max(y[,2]))` ?

Sorry I missed that one. Those reductions (min/max/etc) are only defined over one-dimensional arrays, so in your case, a workaround would be:

```no-highlight
int k = max(to_array_1d(y))

```

The `to_array_1d` call will induce a copy, so if you want to maximise performance you could move this to the `transformed data` block.

> If you don’t mind I have one other question… If I have `vector ff(vector a, row_vector[] X)` I see that I must use `int m = num_elements(a)` to obtain the length of the vector `a` . To get the number of columns of `X` I would have thought that `int p = cols(X)` would have worked, but I get `no matches for cols(row_vector[]) ... available signatures for cols .. cols(row_vector)` . Any pointers appreciated.

Because X is a one-dimensional array of row-vectors (`row_vector[]`), there isn’t a concept of rows/columns of an array to count. As Stan doesn’t (yet) allow ragged arrays, every row vector in the array will have to have the same number of columns, so you can use:

```no-highlight
int p = cols(X[1])

```

If instead you wanted the number of row vectors in the array, you can use `size(X)`

---

_[View the full topic](https://discourse.mc-stan.org/t/how-to-declare-and-use-integer-matrices/16263)._
