# Accessing columns from array of vectors

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
}

}``````

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);
}
``````

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:

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

It’d be a bit more efficient with a temp

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

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?

1 Like

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.

### 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` , produces a row vector result. That is, `m` produces the same result as `m[3, ]` or `m[3, 1:cols(m)]` .