# Complex matrix structure

**URL:** https://discourse.mc-stan.org/t/complex-matrix-structure/1119
**Category:** General
**Tags:** stanc
**Created:** [July 2, 2017, 12:37am UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119 "2017-07-02T00:37:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Bruce\_Ho](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@Bruce\_Ho](https://discourse.mc-stan.org/u/Bruce_Ho)
#### Post date: [July 2, 2017, 12:37am UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119/1 "2017-07-02T00:37:27Z")

</div>

Is it possible to declare custom structures in Stan? For example, I am working with a dynamic HMM problem. There are 3 states, so I need a 3 x 3 matrix to contain all the parameters pertinent to each transition. Each transition has one intercept term xi, and 6 coefficients beta[6]. Is there an efficient way to capture all these in one matrix declaration and be able to work with it?

---

<div class="post-metadata">

### Author: ![Bruce\_Ho](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@Bruce\_Ho](https://discourse.mc-stan.org/u/Bruce_Ho)
#### Post date: [July 2, 2017, 3:44am UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119/2 "2017-07-02T03:44:56Z")

</div>

I think

`matrix[3, 3] beta[k]`

would work.

---

<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: [July 2, 2017, 8:28pm UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119/3 "2017-07-02T20:28:37Z")

</div>

That gives you indexing `beta[k, i, j]` if that’s what you want, or `beta[k]` to pick out a matrix.

The main problem with HMMs is that those matrices have to be simplex constrained if they’re the transition matrices, so you want to use something like:

```
simplex[3] beta[K, 3];

```

Same indexing structure, `beta[k, i, j]`, but now you are guaranteed to have a simplex. If you’re defining the matrix yourself using something like softmax over predictors, this would only give you error checking.

---

<div class="post-metadata">

### Author: ![bhomass](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bhomass/32/407_2.png) [@bhomass](https://discourse.mc-stan.org/u/bhomass)
#### Post date: [July 3, 2017, 4:50pm UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119/4 "2017-07-03T16:50:59Z")

</div>

Vector[3,3] beta is exactly what I need for this. I don’t really need the matrix properties except an efficient way to access the beta vectors using row and column.

---

<div class="post-metadata">

### Author: ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)
#### Post date: [July 10, 2017, 4:30am UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119/5 "2017-07-10T04:30:01Z")

</div>

> [@bhomass](#):
>
> Vector[3,3] beta is exactly what I need for this.

I’m confused. That won’t work. A `vector` only has one index.

---

<div class="post-metadata">

### Author: ![bhomass](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bhomass/32/407_2.png) [@bhomass](https://discourse.mc-stan.org/u/bhomass)
#### Post date: [July 10, 2017, 5:15am UTC](https://discourse.mc-stan.org/t/complex-matrix-structure/1119/6 "2017-07-10T05:15:25Z")

</div>

to be exact, I am using

vector[K] beta[S,S];
