# Matrix multiplication

**URL:** <https://discourse.mc-stan.org/t/matrix-multiplication/3786>\
**Category:** General\
**Created:** [April 5, 2018, 5:29am UTC](https://discourse.mc-stan.org/t/matrix-multiplication/3786 "2018-04-05T05:29:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmendesb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/gmendesb/32/1649_2.png) [@gmendesb](https://discourse.mc-stan.org/u/gmendesb)\
**Post date:** [April 5, 2018, 5:29am UTC](https://discourse.mc-stan.org/t/matrix-multiplication/3786/1 "2018-04-05T05:29:52Z")

</div>

Hi all,  
I’m trying to do the following matrix multiplication in the transformed parameters block: K2[C] = A[C,C] \* K1[C] for several samples S.

> transformed parameters{  
> vector [S] K1[C];  
> vector [S] K2[C];  
> matrix [C,C] A[S];
> 
> for (s in 1:S) {  
> K2[s] = A[s] \* K1[s];  
> }  
> }

A is supposed to be a one dimensional array of size S containing C × C matrices and K1 and K2 arrays of size S containing vectors of size C.

I’ve tried many ways to do that, but they all result in error. I’m happy to provide more information if needed.

Thanks

---

<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:** [April 5, 2018, 5:33am UTC](https://discourse.mc-stan.org/t/matrix-multiplication/3786/2 "2018-04-05T05:33:52Z")

</div>

> [@gmendesb](#):
>
> K1 and K2 arrays of size S containing vectors of size C.

That is inconsistent with how you declared them; that should be

```no-highlight
transformed parameters {
  vector[C] K1[S];
  vector[C] K2[S];
  ...
}

```

---

<div class="post-metadata">

**Author:** ![gmendesb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/gmendesb/32/1649_2.png) [@gmendesb](https://discourse.mc-stan.org/u/gmendesb)\
**Post date:** [April 5, 2018, 6:43am UTC](https://discourse.mc-stan.org/t/matrix-multiplication/3786/3 "2018-04-05T06:43:30Z")

</div>

Thanks so much!
