# Vector to integer formatting

**URL:** <https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690>\
**Category:** Modeling\
**Tags:** rstan\
**Created:** [July 27, 2021, 10:39pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690 "2021-07-27T22:39:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![McLeodM](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@McLeodM](https://discourse.mc-stan.org/u/McLeodM)\
**Post date:** [July 27, 2021, 10:39pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/1 "2021-07-27T22:39:13Z")

</div>

I am making a user-defined function and am trying to take a random sample from values 1:100 and then use that random sample to index a matrix.This is how I would write it in R.

```stan
Ind = sample(1:100, 100)
    B[,1] = A[, Ind]

```

But I am running into issues with taking a vector of real numbers and making them into an integer. Is there a function that I am missing like ‘as.integer’ ? Additionally there is not a function in Stan to replace ‘sample()’ so I have gone with uniform\_rng. This is what I have translated so far. Any suggestions?

```stan
vector[100] x;
int Ind[100];

x = uniform_rng(1, Q);
Ind = x;
    B[,1] = A[,Ind] ;

```

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [July 28, 2021, 7:50am UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/2 "2021-07-28T07:50:47Z")

</div>

The most recent Stan has `discrete_range_rng`.

```stan
int x = discrete_range(1, 100);

```

Alternatively `beta_binomial_rng(Q,1,1)` generates uniform samples from 0:Q.

```stan
int x = 1 + beta_binomial_rng(99, 1, 1);

```

---

<div class="post-metadata">

**Author:** ![McLeodM](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@McLeodM](https://discourse.mc-stan.org/u/McLeodM)\
**Post date:** [July 28, 2021, 6:06pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/3 "2021-07-28T18:06:49Z")

</div>

> [@nhuurre](#):
>
> `binomial_rng(99, 1, 1);`

thank you @nhuurre. That solved my integer and sampling problem! I am finding myself with another vector that is derived from summing a column in a matrix that I also want to make into an integer for indexing another matrix. i.e.

```stan
matrix[100, 100] z;

for ( i in 1:100) {
   y[i] = sum(x[,i]);
}
   z[,1] = y;

```

Y is made up of integers but outputted as reals. Are there any functions for converting reals into integers?

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [July 28, 2021, 6:12pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/4 "2021-07-28T18:12:40Z")

</div>

Just double-checking: you know that these functions can only be used in the TD and GQ sections, right? Stan doesn’t permit any kind of randomness in the TP & model blocks.

---

<div class="post-metadata">

**Author:** ![McLeodM](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@McLeodM](https://discourse.mc-stan.org/u/McLeodM)\
**Post date:** [July 28, 2021, 6:18pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/5 "2021-07-28T18:18:12Z")

</div>

Hi @mike-lawrence , Yes I am aware, thanks.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [July 28, 2021, 6:35pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/6 "2021-07-28T18:35:13Z")

</div>

> [@McLeodM](#):
>
> Y is made up of integers but outputted as reals. Are there any functions for converting reals into integers?

No, there aren’t. If `x` (and `y`) is integer data then you should declare it as `array[N,M] int` instead of `matrix[N,M]`. If `x` is a parameter matrix then you can’t do this inside the Stan model.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [July 28, 2021, 6:48pm UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/7 "2021-07-28T18:48:04Z")

</div>

> [@McLeodM](#):
>
> I am finding myself with another vector that is derived from summing a column in a matrix that I also want to make into an integer for indexing another matrix. i.e.

Remember, as in the other post, to work backwards from the type you need to ultimately use downstream. As @nhuurre says, if you need integers then work backwards from that and don’t store anything upstream in a matrix, but rather in an integer array.

However, in the weird case where you actually need to multiply/invert/decompose matrices in order to generate integers to be used for indexing _exclusively in_ `generated quantities` or `transformed data` (where real-to-integer conversion won’t break the HMC sampler), then you could use the hacks discussed in the link below to accomplish what you need. However, once you reach that point I strongly suspect that it would be simpler to precompute the transformed data (e.g. in R or python) and pass them as data, or to post-compute the generated quantities (e.g. in R or python) from the fitted model object.

> [@Real to integer conversion?](https://discourse.mc-stan.org/t/real-to-integer-conversion/5622):
>
> Can you or are there any plans for introducing real to integer conversion? In my use case I’ve an array of pre-computed matrices: matrix[20, 20] A\_inv[10]. I would like to be able to choose a matrix like A\_inv[idx], where idx is calculated from real variables, eq idx = to\_integer(ceil(x/5.0)). The reference manual 2.17 chapter 41.7 states: “The rounding functions cannot be used as indices to arrays because they return real values. Stan may introduce integer-valued versions of these in the futu…

---

<div class="post-metadata">

**Author:** ![McLeodM](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@McLeodM](https://discourse.mc-stan.org/u/McLeodM)\
**Post date:** [July 30, 2021, 12:40am UTC](https://discourse.mc-stan.org/t/vector-to-integer-formatting/23690/8 "2021-07-30T00:40:52Z")

</div>

Thanks @nhuurre and @jsocolar and for all your input. I would have truly benefited from starting at the end of my function when I started started translating R.
