# Passing empty list/array of matrices as data

**URL:** https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883
**Category:** RStan
**Created:** [June 12, 2017, 11:52pm UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883 "2017-06-12T23:52:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Knowles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/david_knowles/32/309_2.png) [@David\_Knowles](https://discourse.mc-stan.org/u/David_Knowles)
#### Post date: [June 12, 2017, 11:52pm UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883/1 "2017-06-12T23:52:28Z")

</div>

If I have data of the form  
`vector[N] x[P];`  
and in a particular use of my model have P=0 I can just pass an 0xN matrix from R to Stan.

However, if I try to do the equivalent with an array of matrices  
`vector[N,N] x[P];`  
passing a 0xNxN array as x then Stan chokes with (here N=10)  
`"mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=x; dims declared=(0,10,10); dims found=(0)"`  
Passing an empty list doesn’t work, and everything is fine with P\>0. It seems at some point the dim attributes of x got lost. Is this a bug? Is there some other way I can pass in the array that would work?

(the context is a model with some optional components. I can work  
around by making additional special case stan files but this is pretty  
cumbersome)

Thanks in advance for any help.

---

<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: [June 12, 2017, 11:58pm UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883/2 "2017-06-12T23:58:22Z")

</div>

Good seeing you on the forums!

In R, have you tried creating a 3-d array? Something like:

```
x <- array(0, dim=c(0, 10, 10))

```

---

<div class="post-metadata">

### Author: ![David\_Knowles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/david_knowles/32/309_2.png) [@David\_Knowles](https://discourse.mc-stan.org/u/David_Knowles)
#### Post date: [June 13, 2017, 12:06am UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883/3 "2017-06-13T00:06:11Z")

</div>

Thanks Dan! 6 minute response time? That’ll do I guess.

Curious. That’s _almost_ exactly what I was doing  
`x <- array(dim=c(0, 10, 10))`  
on the basis that the data didn’t matter. This crashes with the above error. It is seems to be equivalent, as you would expect, to  
`x <- array(NA, dim=c(0, 10, 10))`  
However, your solution  
`x <- array(0, dim=c(0, 10, 10))`  
runs just fine. I’ve no idea what the difference is: as far as I can tell the two objects in R are identical (class, dims, no other attributes).

---

<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: [June 13, 2017, 9:12pm UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883/4 "2017-06-13T21:12:50Z")

</div>

Sorry, that’s deep in the internals and I don’t really have an clue on what’s going on.

---

<div class="post-metadata">

### Author: ![helske](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/helske/32/3376_2.png) [@helske](https://discourse.mc-stan.org/u/helske)
#### Post date: [June 13, 2017, 9:37pm UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883/5 "2017-06-13T21:37:33Z")

</div>

Might be due to the fact that he first two cases create logical arrays, whereas the last one is a numeric array.

---

<div class="post-metadata">

### Author: ![David\_Knowles](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/david_knowles/32/309_2.png) [@David\_Knowles](https://discourse.mc-stan.org/u/David_Knowles)
#### Post date: [June 13, 2017, 9:56pm UTC](https://discourse.mc-stan.org/t/passing-empty-list-array-of-matrices-as-data/883/6 "2017-06-13T21:56:10Z")

</div>

Yup I think you’re correct, seems to be the only difference between the resulting objects. Odd to me that `array` returns a logical rather than a numeric by default but I’ll just add it to the list of R’s idiosyncrasies.
