# Help understanding what's happening in this code

**URL:** <https://discourse.mc-stan.org/t/help-understanding-whats-happening-in-this-code/11296>\
**Category:** General\
**Created:** [October 8, 2019, 2:34pm UTC](https://discourse.mc-stan.org/t/help-understanding-whats-happening-in-this-code/11296 "2019-10-08T14:34:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgewambold](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@georgewambold](https://discourse.mc-stan.org/u/georgewambold)\
**Post date:** [October 8, 2019, 2:34pm UTC](https://discourse.mc-stan.org/t/help-understanding-whats-happening-in-this-code/11296/1 "2019-10-08T14:34:53Z")

</div>

Hello, I’m new to Stan and I have a question about something I saw when reading some research.

The model block had code that looked like this:  
`some_vector[1] = some_matrix[1,] * transpose(some_other_matrix[1,])`

This suggests that the product of a row slice of a matrix and a transposed(row slice) is a scalar. Is this correct? Does `matrix[n,]` return a row\_vector?

I created a code snippet that can be run in R to print what was happening:

```
model_code = "
data{
matrix[4,2] test_matrix;
}
transformed data{
print(test_matrix[1,]);
print(transpose(test_matrix[1,]));
print(test_matrix[1,] * transpose(test_matrix[1,]));
}
parameters{
}
model{
}
"

data = list(test_matrix=matrix(1:9, nrow = 4, ncol = 2))

stan(model_code = model_code, data = data, algorithm = 'Fixed_param', chains = 1)

```

Which prints:

```
[1,5]
[1,5]
26

```

If someone could lend some clarity to what’s happening, I would appreciate it! Cheers.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 8, 2019, 3:08pm UTC](https://discourse.mc-stan.org/t/help-understanding-whats-happening-in-this-code/11296/2 "2019-10-08T15:08:49Z")

</div>

> [@georgewambold](#):
>
> This suggests that the product of a row slice of a matrix and a transposed(row slice) is a scalar. Is this correct? Does `matrix[n,]` return a row\_vector?

Yes to both, that’s correct.

Think of this as a product between a 1xN matrix and an Nx1 matrix (the Nx1 matrix comes from the 1xN transposed). The result there will be 1x1. Does that clarify things at all?

The printed output agrees with this.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [October 8, 2019, 3:10pm UTC](https://discourse.mc-stan.org/t/help-understanding-whats-happening-in-this-code/11296/3 "2019-10-08T15:10:12Z")

</div>

Oh maybe I interpreted this incorrectly.

Is the alternative you might expect a 1x1 matrix, but Stan gave you a scalar?

---

<div class="post-metadata">

**Author:** ![georgewambold](https://avatars.discourse-cdn.com/v4/letter/g/f14d63/32.png) [@georgewambold](https://discourse.mc-stan.org/u/georgewambold)\
**Post date:** [October 8, 2019, 4:05pm UTC](https://discourse.mc-stan.org/t/help-understanding-whats-happening-in-this-code/11296/4 "2019-10-08T16:05:45Z")

</div>

No you got it right, I just wasn’t sure about the type of the matrix slice. In my head I was thinking `product between a 1xN matrix and an Nx1` would be 1xN too, I was mistaken about that :)

Thanks for clarifying.
