# [R] extract positions from matrix

R. Michael Weylandt michael.weylandt at gmail.com
Sat Nov 19 15:32:40 CET 2011

```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:
> Hi everyone, I tried to solve this problem but I could not find the
> solution. I have about 105 matrices of equal size in the memory of**R, I
> need to do is extract from these matrices some known positions and
> create a new matrix with these columns. Show you an example with only
> three matrices (but in my case I have hundreds of them).
>
> A=matrix(1:15,5)
>
> [,1] [,2] [,3]
>
> [1,] 1 6 11
>
> [2,] 2 7 12
>
> [3,] 3 8 13
>
> [4,] 4 9 14
>
> [5,] 5 10 15
>
> B=matrix(15:29,5)
>
> [,1] [,2] [,3]
>
> [1,] 15 20 25
>
> [2,] 16 21 26
>
> [3,] 17 22 27
>
> [4,] 18 23 28
>
> [5,] 19 24 29
>
> C=matrix(30:44,5)
>
> [,1] [,2] [,3]
>
> [1,] 30 35 40
>
> [2,] 31 36 41
>
> [3,] 32 37 42
>
> [4,] 33 38 43
>
> [5,] 34 39 44
>
> The positions I wish to extract are 1 and 5 of the first row of each
> matrix (in my case are 25positions) and with this generate a new matrix
> with the form
>
> d=
>
> [,1] [,2] [,3]
>
> [1,] 1 15 30
>
> [2,] 5 19 34
>
> The ideais to builda loop toextract thisinformation from
> hundredsmatrices, butI failed todo so.
>
> Any helpwould be greatthank you very muchin advance
>
> regards
> .jpg
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

```