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
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
to Tor Arne Øigård, TMB Users

The relevant documentation is

https://kaskr.github.io/adcomp/structarray.html

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.



From: tmb-...@googlegroups.com <tmb-...@googlegroups.com> on behalf of Tor Arne Øigård <torarne...@gmail.com>
Sent: Wednesday, February 7, 2018 3:05:21 PM
To: TMB Users
Subject: [TMB users] indexing of multi-dimensional array (3D and beyond)
 
--
To post to this group, send email to us...@tmb-project.org. Before posting, please check the wiki and issuetracker at https://github.com/kaskr/adcomp/. Please try to create a simple repeatable example to go with your question (e.g issues 154, 134, 51). Use the issuetracker to report bugs.
---
You received this message because you are subscribed to the Google Groups "TMB Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tmb-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tmb-users/94bbc6cf-afc5-49a4-a666-2e739e192995%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tor Arne Øigård

unread,
Feb 8, 2018, 9:21:48 AM2/8/18
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?
Reply all
Reply to author
Forward
0 new messages