indexing of multi-dimensional array (3D and beyond)
312 views
Skip to first unread message
Tor Arne Øigård
unread,
Feb 7, 2018, 9:05:21 AM2/7/18
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to TMB Users
Hi,
I've looked in the documentation and these questions on the Google group about accessing rows and columns in arrays of order 3 and beyond but could not find much on this. Maybe because it is so trivial? :-). As far as I understand arrays of dimension > 2 is not developed in the Eigen library but specifically developed for TMB. I also looked in the matrix_arrays_8cpp-example which has been very useful in many other cases but could not find anything on it there.
Say for example I have a matrix M of dimension I x J, I can access row i by M.row(i).
But what if I have an array A of dimension 3. Say dim of A is I x J x K and I would like to extract a vector as a function of j given row i and the 3rd dimension k and get a vector as a function of j.
My initial guess was newvector = A.row(i,,k) but that does not seem to work. Have tried several other variations without success.
Kind regards, Tor
Kasper Kristensen
unread,
Feb 7, 2018, 10:43:23 AM2/7/18
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
Due to the memory layout of arrays there's only a column extractor. To get the R equivalent of A[i,,k] one would have to do:
A.col(k).transpose().col(i)
There's some overhead in this construction so it's worth permuting the array if slicing has to be done for many i,j combinations. Use either the 'perm' or 'rotate' method.
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to TMB Users
Thanks for this, it worked! Also thanks for the link to the documentation. I saw that by using A.matrix() it keeps the first dimension and collapses the others. I might get away with that somehow and extract a row from the collapsed matrix. Would that be a better solution?