How to fill matrix columns from array of vectors?

198 views
Skip to first unread message

Nikolay Evdokimov

unread,
Jan 12, 2016, 7:44:08 AM1/12/16
to Stan users mailing list
Hello, I want to fill my matrix from array of vectors by appending or assinging them.
(the code is just to show the problem)

matrix[rowsN, factorsCount] allFactors;

from some source data 
vector[rowsN] factors[factorsCount];

ways to do it could be:

for(i in 1:factorsCount)
{
       allFactors[,i] =factors[i] ;
}

or may be I should declare empty matrix and then concatenate it with vectors...

matrix[rowsN, factorsCount] allFactors;
for(i in 1:factorsCount)
{
       allFactors =append_col(allFactors, factors[i]) ;
}

Both approaches give error from the compiler:

"Not a legal assignment, sampling, or function statement.  Note that
  * Assignment statements only allow variables (with optional indexes) on the left;
    if you see an outer function logical_lt (<) with negated (-) second argument,
    it indicates an assignment statement A <- B with illegal left
    side A parsed as expression (A < (-B))."


Which is the correct way to fill matrix from array of vectors?

Regards,
Nikolay

Nikolay Evdokimov

unread,
Jan 12, 2016, 8:14:17 AM1/12/16
to Stan users mailing list
oh, sorry.
 allFactors[,i] <-slowFactors[i];
seems to work fine;

just misused = and <-

Bob Carpenter

unread,
Jan 12, 2016, 2:42:19 PM1/12/16
to stan-...@googlegroups.com
Can you show us a real example. For the toy example you gave, the
right approach would be to declare the variable with the shape you
want to begin with.

And it's "<-" not "=" for assignment in Stan.

- Bob
> --
> 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.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

David Manheim

unread,
Jan 19, 2016, 8:53:09 PM1/19/16
to Stan users mailing list
One real example I've been hit with is when I need to re-construct a matrix inside of the ODE, since I can't pass it matrix data types :(

Bob Carpenter

unread,
Jan 20, 2016, 6:05:29 PM1/20/16
to stan-...@googlegroups.com
Although that's not an array of vectors, either, we're
sympathetic to that problem. We need functions that will
start at index N in an array and peel off any given Stan
data type. We have all that in C++ as part of our I/O,
but not exposed as functions in Stan. But it's on our to-do list.

For now, you can use the new slicing functions, though it
looks like there are still some include issues that's blocking
the full functionality there.

- Bob
Reply all
Reply to author
Forward
0 new messages