Skip to content

extract positions from matrix

6 messages · .Jpg, R. Michael Weylandt, David Winsemius +1 more

1 day later
#
Here's one approach:

A=matrix(1:15,5)
B=matrix(15:29,5)
C=matrix(30:44,5)

do.call(cbind, lapply(c("A","B","C"),function(x) get(x)[c(1,5),1]))

Michael
On Thu, Nov 17, 2011 at 9:44 PM, .Jpg <jporobicg at gmail.com> wrote:
#
On Nov 19, 2011, at 9:32 AM, R. Michael Weylandt wrote:

            
Also:

sapply( list(A,B,C), function(x) do.call("[", list(x, c(1,5)))  )

Notice that this actually was extracting using what might be called  
the "vector positions". If you wanted to use the i,j version of "["  
then you would need an extra column (this example pulling the second  
columns in the rows selected:

 > sapply(list(A,B,C),function(x) do.call("[", list(x, c(1,5), 2)) )
      [,1] [,2] [,3]
[1,]    6   20   35
[2,]   10   24   39


Comments on the differences: Michaels version used cbind to get the  
ruslt in a matrix, whereas mine used sapply. His used the get function  
to extract the objects from a character vector, whereas mine never  
constructed a character vector. Which one you deploy will depend on  
your data setup. If you already have these in a list, mine might be  
easier, but if they constitute an easily constructed set of names you  
might use LETTERS[] numbers and paste() to build your list of object  
names.
#
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:

  
    
#
On Nov 19, 2011, at 1:15 PM, Bert Gunter wrote:

            
I suggested that my approach was most applicable if the questioner had  
his matrices already in a list. (He didn't say, so neither of us knows  
yet what his/her use case is.)
I wonder if you are doing ".jpg" a favor by putting ls() at the center  
of your strategy unless you offer the tools to isolate these objects  
from the rest of the global environment. Who works with no other  
objects in the workspace?

Perhaps this would add the extra level of encapsulation needed for the  
sapply(ls() method of access the created matrices in a probably less  
disruptive fashion:

e <- new.env()
with(e, { for(i in 1:3) assign( LETTERS[i], 15*(i-1)+seq_len(15) )
rm(i)
ar <-  array( sapply(ls(), get), dim=c(5,3,3)) })

# ----------------
e$ar[c(1,5),1, ]

      [,1] [,2] [,3]
[1,]    1   16   31
[2,]    5   20   35
#
Inline below. -- Bert
On Sat, Nov 19, 2011 at 1:06 PM, David Winsemius <dwinsemius at comcast.net> wrote:
I think this is a good point. The real issue is how to get the
"hundreds of matrices" into a friendly data structure. If they were
already in a list, for example, then it would be even simpler to
convert them into an array, which I still think is the right data
structure for this sort of extraction.

Cheers,
Bert