real b;
vector[N] c;
real d;
int ndx;
If N == 2, as in your example,
d <- ifelse(b < 2.0, c[1], c[2]);
If N > 2,
ndx <- 1;
for (i in 2:N) {
if (i <= floor(b)
ndx <- i;
}
d <- c[ndx];
;-)
Sergio
Il 13/09/2013 07:11, Kyle Foreman ha scritto:
> Hello all,
>
> Is there any way to coerce a real value into an integer? I need to use a
> real-valued parameter as the index for a vector.
>
> Simple type coercion isn't allowed:
> |
> inta;
> real b;
>
> b <-1.3;
> a <-floor(b);// type mismatch error
> |
>
> And using a real for an index of course also fails:
> |
> real a;
> real b;
> vector[2]c;
> real d;
>
> b <-1.3;
> a <-floor(b);// works
> d <-c[b];// error "expression denoting integer required; found type=real"
> |
>
> Any suggestions on how to take a rounded real number and retrieve a
> vector element with it?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google
> Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
stan-users+...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.