accessing a matrix column as a vector?

1,452 views
Skip to first unread message

Douglas Bates

unread,
Mar 18, 2015, 2:18:43 PM3/18/15
to stan-...@googlegroups.com
There may be a better way of doing what I want to because I am not in a correct Stan mindset.  Details of what I am trying to do are in the StanVectorOne notebook at


Depending on how I formulate the variables the task I want to accomplish is to access a column of a matrix as a vector or use a 1-D array of vectors or ...

I have looked through the manual but haven't found a description of accessing a column of a matrix as a vector.  Did I miss something completely?

Douglas Bates

unread,
Mar 18, 2015, 2:20:01 PM3/18/15
to stan-...@googlegroups.com

Douglas Bates

unread,
Mar 18, 2015, 2:23:42 PM3/18/15
to stan-...@googlegroups.com
On Wednesday, March 18, 2015 at 1:20:01 PM UTC-5, Douglas Bates wrote:
After going back and reading that notebook again I realized that I have a trivial mistake in the problematic statement.  So no need to investigate.

I still would appreciate learning if there is a way to access a matrix column as a vector.

Ben Goodrich

unread,
Mar 18, 2015, 2:32:22 PM3/18/15
to stan-...@googlegroups.com
On Wednesday, March 18, 2015 at 2:23:42 PM UTC-4, Douglas Bates wrote:
I still would appreciate learning if there is a way to access a matrix column as a vector.

Probably

col(X, k)

is what you need to extract the k-th column of X. But it can be more efficient to represent a NxK "matrix" as a K-array of N-vectors

vector[N] X[K];

in which case you could just access the k-th "column" with X[k]. Bob has been working on some R-like syntax --- such as X[1:N,k] --- but it is not merged yet.

Ben

Douglas Bates

unread,
Mar 18, 2015, 2:46:10 PM3/18/15
to stan-...@googlegroups.com
Thanks, Ben.  The 1-D array of vectors was the approach I eventually adopted. 

Bob Carpenter

unread,
Mar 18, 2015, 7:26:24 PM3/18/15
to stan-...@googlegroups.com
See inline.

> On Mar 19, 2015, at 5:46 AM, Douglas Bates <dmb...@gmail.com> wrote:
>
> On Wednesday, March 18, 2015 at 1:32:22 PM UTC-5, Ben Goodrich wrote:
> On Wednesday, March 18, 2015 at 2:23:42 PM UTC-4, Douglas Bates wrote:
> I still would appreciate learning if there is a way to access a matrix column as a vector.
>
> Probably
>
> col(X, k)
>
> is what you need to extract the k-th column of X. But it can be more efficient to represent a NxK "matrix" as a K-array of N-vectors
>
> vector[N] X[K];
>
> in which case you could just access the k-th "column" with X[k]. Bob has been working on some R-like syntax --- such as X[1:N,k] --- but it is not merged yet.

I'll allow X[,k], too. But it still will require a copy until
we sort out all the Eigen pass-by-expression-template stuff.

>
> Thanks, Ben. The 1-D array of vectors was the approach I eventually adopted.

Good move --- it allows a reference to be returned.

Eventually, we'll get all the Eigen expression templates working
so that col(X,k) will return a view of the column, which will be
memory local by default because we're using Eigen's default
column-major memory layout.

- Bob

Steve Bellan

unread,
Jun 25, 2015, 11:31:42 AM6/25/15
to stan-...@googlegroups.com
Hi Ben,

I was just dealing with the same issue and glad i found the answer. However, I'm confused that you suggest an array of vectors since I thought traversing columns in a matrix was more efficient than traversing objects within an array. Or am I getting something wrong?

I'm pulling out a few columns from a matrix of columns where each row is from a multivariate normal distribution. I assume I need to keep it as a matrix and not change to an array of column vectors if the rows are from multivariate distribution. That correct?

Thanks,

Steve


On Wednesday, March 18, 2015 at 1:32:22 PM UTC-5, Ben Goodrich wrote:

Steve Bellan

unread,
Jun 25, 2015, 11:44:12 AM6/25/15
to stan-...@googlegroups.com
Actually, it seems that col(X,k) doesn't want to take a vector of integers for k. Is it not possible to take out a few columns of a matrix at once?

Ben Goodrich

unread,
Jun 25, 2015, 1:40:49 PM6/25/15
to stan-...@googlegroups.com, steve....@gmail.com
On Thursday, June 25, 2015 at 11:44:12 AM UTC-4, Steve Bellan wrote:
Actually, it seems that col(X,k) doesn't want to take a vector of integers for k. Is it not possible to take out a few columns of a matrix at once?

Yes, with the block() function.

Ben

Steve Bellan

unread,
Jun 25, 2015, 4:08:29 PM6/25/15
to stan-...@googlegroups.com, steve....@gmail.com
awesome thanks Ben!
Reply all
Reply to author
Forward
0 new messages