Skip to content
Prev 277932 / 398506 Next

extract positions from matrix

Folks:

David: I believe your approach needs to be modified to accommodate
hundreds of matrices per the OP's specification.

However, I would say that this is exactly the situation in which
arrays should be used. Not only does it appear simpler, but it also
could be faster since once the array is created, (vectorized) indexing
is used. The key is to take advantage of column major ordering of
arrays in R as follows:

1. The OP's example specification is wrong -- he wants positions 1 and
5 of the first COLUMN of each matrix (which both solutions gave, but
just didn't mention).

2. The code below creates the array assuming that the global workspace
has only the matrices in it. Subscript ls() appropriately if this is
not the case.
[,1] [,2] [,3]
[1,]    1   16   31
[2,]    5   20   35


Cheers,
Bert
On Sat, Nov 19, 2011 at 9:19 AM, David Winsemius <dwinsemius at comcast.net> wrote: