# Accessing columns from array of vectors

**URL:** <https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057>\
**Category:** Modeling\
**Created:** [January 19, 2018, 8:28pm UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057 "2018-01-19T20:28:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@chris](https://discourse.mc-stan.org/u/chris)\
**Post date:** [January 19, 2018, 8:28pm UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057/1 "2018-01-19T20:28:10Z")

</div>

I’m sure this is something simple, but I can’t seem to find an example in the Stan manual nor the Discourse topics.

Basically, I want to be able to access (take a slice) the columns from an array of vectors. I’m not using a matrix because I need the simplex constraints.

Here is a snippet of what I’m trying to do. Please forgive the “:” python-like array accessor, that’s there to indicate the slice I want to take.

```
transformed parameters {

   simplex[V] phi[K]; // word dist for topic k
   simplex[K] rho[V]; //noise

   //setup the topic distribution as a simplex over words
   for (k in 1:K) {
      phi[k] = softmax(iota[k]);
   }

   // normalize rho as a simplex over topics
   for (v in 1:V) {
      rho[v] = phi[:,v]/sum(phi[:,v]); // THIS IS THE OPERATION THAT I'M NOT SURE HOW TO FORMAT
   }

}
```

---

<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:** [January 19, 2018, 9:01pm UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057/2 "2018-01-19T21:01:31Z")

</div>

It is closer to R syntax in that it does not use the colon operator the way it is in Python. But I think you need two loops

```
for (v in 1:V) {
  vector[K] temp;
  for (k in 1:K) temp[k] = phi[k,v];
  rho[v] = temp / sum(temp);
}

```

---

<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:** [February 6, 2018, 3:24am UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057/3 "2018-02-06T03:24:36Z")

</div>

> [@bgoodri](#):
>
> it does not use the colon operator the way it is in Python

You can use the `:` if you like. Both bounds are optional and taken to be extremal if elided. It just seemed to make the most sense as edge cases, and I think it’s also what MATLAB does.

But I’d skip the inner loop and write this:

```no-highlight
for (v in 1:V)
  rho[v] = phi[, v] / sum(phi[, v]);

```

It’d be a bit more efficient with a temp

```no-highlight
for (v in 1:V) {
  vector[V] phi_col_v = phi[, v];
  rho[v] = phi_col_v / sum(phi_col_v);
}

```

---

<div class="post-metadata">

**Author:** ![tds151](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/tds151/32/5300_2.png) [@tds151](https://discourse.mc-stan.org/u/tds151)\
**Post date:** [February 4, 2020, 6:34pm UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057/4 "2020-02-04T18:34:59Z")

</div>

Is the syntax, phi[, v], valid for selecting a column of a matrix in Stan and may it also be used for assignment? Is there a reason (e.g., inefficiency) why this syntax option is not documented?

---

<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:** [February 4, 2020, 7:46pm UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057/5 "2020-02-04T19:46:22Z")

</div>

It is valid. If it is not documented, I don’t know why. It can be more efficient to declare an array of column vectors if you are only doing columnwise operations and no linear algebra, but it isn’t a huge deal either way.

---

<div class="post-metadata">

**Author:** ![spinkney](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@spinkney](https://discourse.mc-stan.org/u/spinkney)\
**Post date:** [February 4, 2020, 8:01pm UTC](https://discourse.mc-stan.org/t/accessing-columns-from-array-of-vectors/3057/6 "2020-02-04T20:01:15Z")

</div>

### Matrices with One Multiple Index

If matrices receive a single multiple index, the result is a matrix. So if `m` is a matrix, so is `m[2:4]` . In contrast, supplying a single index, `m[3]` , produces a row vector result. That is, `m[3]` produces the same result as `m[3,]` or `m[3, 1:cols(m)]` .

from [https://mc-stan.org/docs/2\_21/stan-users-guide/multiple-indexes-with-vectors-and-matrices.html](https://mc-stan.org/docs/2_21/stan-users-guide/multiple-indexes-with-vectors-and-matrices.html)
