# Passing matrix with one dimension equal to 0 works only sometimes?

**URL:** https://discourse.mc-stan.org/t/passing-matrix-with-one-dimension-equal-to-0-works-only-sometimes/11732
**Category:** Modeling
**Created:** [November 4, 2019, 12:43pm UTC](https://discourse.mc-stan.org/t/passing-matrix-with-one-dimension-equal-to-0-works-only-sometimes/11732 "2019-11-04T12:43:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Merlin\_Heidemanns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/merlin_heidemanns/32/6159_2.png) [@Merlin\_Heidemanns](https://discourse.mc-stan.org/u/Merlin_Heidemanns)
#### Post date: [November 4, 2019, 12:43pm UTC](https://discourse.mc-stan.org/t/passing-matrix-with-one-dimension-equal-to-0-works-only-sometimes/11732/1 "2019-11-04T12:43:46Z")

</div>

Why can I pass a matrix with one dimension equal to 0 to Stan in one case but not in another? The example below works (but shouldn’t?) while in another case it unapologetically states that

```
  Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=x_e; dims declared=(150,0); dims found=(0) (in 'model_msm_constant_continuous_v2' at line 15)

```

Working example:

```
library(rstan)

m <- rstan::stan_model("test.stan")

N <- 300
K <- 0
x <- matrix(rbinom(N * K, 1, 0.3), ncol = K, nrow = N)
alpha <- rnorm(1, 0, 1)
beta <- rnorm(K, 0, 1)
sigma <- abs(rnorm(1, 0, 1))
y <- rnorm(N, alpha + x %*% beta, sigma)

data <- list(
  N = N,
  K = K,
  x = x,
  y = y
)

fit <- rstan::sampling(m, data = data, chains = 2, iter = 1000)

```

```stan
data {
  int N;
  int<lower = 0> K;
  matrix[N, K] x;
  vector[N] y;
}

transformed data {
  int K_ = K == 0 ? 1 : K;
  matrix[N, K_] x_ = K == 0 ? rep_matrix(0.0, N, K_) : x;
  print(K);
  print(x);
}

parameters {
  real alpha;
  vector[K_] beta;
  real<lower = 0> sigma;
}

model {
  target += normal_lpdf(beta | 0, 1);
  target += normal_lpdf(y | alpha + x_ * beta, sigma);
}

```

This breaks if Mx\_e is equal to 0 and x\_e[150, Mx\_e]

```
data {
  // dimensions and slicing
  int<lower = 1> N; // observations
  int<lower = 2> T; // time points
  int<lower = N * T> NT; // product
  int startstop[N, 2]; // slicer
  int<lower = 2> K; // N(states)

  // predictor dimensions
  int<lower = 0> Mx_d; // N(varying parameters of continuous process)
  int<lower = 0> Mx_e; // N(varying parameters of continuous process)

  // predictors
  matrix[NT, Mx_d] x_d; // matrix of shared continuous of discrete process
  matrix[NT, Mx_e] x_e; // matrix of varying continuous of discrete process

  // output
  vector[NT] y; // vector of output

  // intercepts
  int has_intercept[5]; // 1: alpha, 2: beta

  // shared
  int shared_TP; // 0: individual, 1: shared
}

transformed data {
  // add intercepts
  int Mx_d_ = has_intercept[1] ? Mx_d + has_intercept[1] : (Mx_d == 0 ? 1 : Mx_d);
  int Mx_e_ = has_intercept[2] ? Mx_e + has_intercept[2] : (Mx_e == 0 ? 1 : Mx_e);
  matrix[NT, Mx_d_] x_d_ = has_intercept[1] ? append_col(rep_vector(1.0, NT), x_d):
                                             (Mx_d == 0 ? rep_matrix(0.0, NT, 1) : x_d);
  matrix[NT, Mx_e_] x_e_ = has_intercept[2] ? append_col(rep_vector(1.0, NT), x_e):
                                             (Mx_e == 0 ? rep_matrix(0.0, NT, 1) : x_e);

  int N_ = shared_TP ? 1 : N;
}

```

---

<div class="post-metadata">

### Author: ![Merlin\_Heidemanns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/merlin_heidemanns/32/6159_2.png) [@Merlin\_Heidemanns](https://discourse.mc-stan.org/u/Merlin_Heidemanns)
#### Post date: [November 4, 2019, 1:41pm UTC](https://discourse.mc-stan.org/t/passing-matrix-with-one-dimension-equal-to-0-works-only-sometimes/11732/2 "2019-11-04T13:41:05Z")

</div>

‘solved’ A matrix with 0 columns and \>0 rows is apparently treated as a matrix of NAs and thereby empty. Creating that empty matrix with any value fixes the problem, e.g.

```
x_d <- matrix(0, ncol = 0, nrow = NT)

```

works but

```
x_d <- matrix(NA, ncol = 0, now = NT)

```

does not despite them looking the same when called.

---

<div class="post-metadata">

### Author: ![hhau](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@hhau](https://discourse.mc-stan.org/u/hhau)
#### Post date: [November 4, 2019, 3:30pm UTC](https://discourse.mc-stan.org/t/passing-matrix-with-one-dimension-equal-to-0-works-only-sometimes/11732/3 "2019-11-04T15:30:39Z")

</div>

Fascinating!

```
> a_na <- matrix(NA, nrow = 0, ncol = 10)
> a_na
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> str(a_na)
 logi[0 , 1:10] 
> b_na <- matrix(0, nrow = 0, ncol = 10)
> b_na
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> str(b_na)
 num[0 , 1:10] 

```

So one of them is a `logical`, whilst the other a `numeric`.

---

<div class="post-metadata">

### Author: ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)
#### Post date: [November 5, 2019, 2:45pm UTC](https://discourse.mc-stan.org/t/passing-matrix-with-one-dimension-equal-to-0-works-only-sometimes/11732/4 "2019-11-05T14:45:41Z")

</div>

Good observations! Just to note that R has `NA_real_` for when you want to specify NAs that are numeric:

```
a_na <- matrix(NA_real_ , nrow = 0, ncol = 10)
str(a_na)
# num[0 , 1:10] 

```
