Subsetting a matrix without for-loop

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Subsetting a matrix without for-loop

Camarda, Carlo Giovanni
Dear R-users,
I'm struggling in R in order to "squeeze" a matrix without using a
for-loop.
Although my case is a bit more complex, the following example should
help you to understand what I would like to do, but without the slow
for-loop.
Thanks in advance,
Carlo Giovanni Camarda


A  <- matrix(1:54, ncol=6)      # my original matrix
A.new <- matrix(nrow=3, ncol=6) # a new matrix which I'll fill
# for-loop
for(i in 1:nrow(A.new)){
    B <- A[i:(i+2), ]   # selecting the rows
    C <- apply(B,2,sum) # summing by columns
    A.new[i,] <- C      # inserting in the new matrix
}


+++++
This mail has been sent through the MPI for Demographic Rese...{{dropped}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Subsetting a matrix without for-loop

Gabor Grothendieck
The result is linear in A so its a matter of finding the matrix to multiply it
by:

 matrix(c(rep(1,3), rep(0,7)), 3, 9, byrow = TRUE) %*% A

On 1/30/06, Camarda, Carlo Giovanni <[hidden email]> wrote:

> Dear R-users,
> I'm struggling in R in order to "squeeze" a matrix without using a
> for-loop.
> Although my case is a bit more complex, the following example should
> help you to understand what I would like to do, but without the slow
> for-loop.
> Thanks in advance,
> Carlo Giovanni Camarda
>
>
> A  <- matrix(1:54, ncol=6)      # my original matrix
> A.new <- matrix(nrow=3, ncol=6) # a new matrix which I'll fill
> # for-loop
> for(i in 1:nrow(A.new)){
>    B <- A[i:(i+2), ]   # selecting the rows
>    C <- apply(B,2,sum) # summing by columns
>    A.new[i,] <- C      # inserting in the new matrix
> }
>
>
> +++++
> This mail has been sent through the MPI for Demographic Rese...{{dropped}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Subsetting a matrix without for-loop

jholtman
In reply to this post by Camarda, Carlo Giovanni
use 'filter':
> x <- matrix(1:100,10)
> x
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> (y <- apply(x, 2, filter, c(1,1,1)))
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
 [2,]    6   36   66   96  126  156  186  216  246   276
 [3,]    9   39   69   99  129  159  189  219  249   279
 [4,]   12   42   72  102  132  162  192  222  252   282
 [5,]   15   45   75  105  135  165  195  225  255   285
 [6,]   18   48   78  108  138  168  198  228  258   288
 [7,]   21   51   81  111  141  171  201  231  261   291
 [8,]   24   54   84  114  144  174  204  234  264   294
 [9,]   27   57   87  117  147  177  207  237  267   297
[10,]   NA   NA   NA   NA   NA   NA   NA   NA   NA    NA
> y[-c(1, nrow(y)),]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    6   36   66   96  126  156  186  216  246   276
[2,]    9   39   69   99  129  159  189  219  249   279
[3,]   12   42   72  102  132  162  192  222  252   282
[4,]   15   45   75  105  135  165  195  225  255   285
[5,]   18   48   78  108  138  168  198  228  258   288
[6,]   21   51   81  111  141  171  201  231  261   291
[7,]   24   54   84  114  144  174  204  234  264   294
[8,]   27   57   87  117  147  177  207  237  267   297
>



On 1/30/06, Camarda, Carlo Giovanni <[hidden email]> wrote:

>
> Dear R-users,
> I'm struggling in R in order to "squeeze" a matrix without using a
> for-loop.
> Although my case is a bit more complex, the following example should
> help you to understand what I would like to do, but without the slow
> for-loop.
> Thanks in advance,
> Carlo Giovanni Camarda
>
>
> A  <- matrix(1:54, ncol=6)      # my original matrix
> A.new <- matrix(nrow=3, ncol=6) # a new matrix which I'll fill
> # for-loop
> for(i in 1:nrow(A.new)){
>    B <- A[i:(i+2), ]   # selecting the rows
>    C <- apply(B,2,sum) # summing by columns
>    A.new[i,] <- C      # inserting in the new matrix
> }
>
>
> +++++
> This mail has been sent through the MPI for Demographic Rese...{{dropped}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



--
Jim Holtman
Cincinnati, OH
+1 513 247 0281

What the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Subsetting a matrix without for-loop

PIKAL Petr
In reply to this post by Camarda, Carlo Giovanni
Hi

> matrix(colSums(embed(A,3)[1:3,]),3,6, byrow=T)[3:1,]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    6   33   60   87  114  141
[2,]    9   36   63   90  117  144
[3,]   12   39   66   93  120  147

will do it. However I am not sure if it is quicker than your for
loop.

HTH
Petr


On 30 Jan 2006 at 16:29, Camarda, Carlo Giovanni wrote:

Date sent:       Mon, 30 Jan 2006 16:29:38 +0100
From:           "Camarda, Carlo Giovanni" <[hidden email]>
To:             <[hidden email]>
Subject:         [R] Subsetting a matrix without for-loop

> Dear R-users,
> I'm struggling in R in order to "squeeze" a matrix without using a
> for-loop. Although my case is a bit more complex, the following
> example should help you to understand what I would like to do, but
> without the slow for-loop. Thanks in advance, Carlo Giovanni Camarda
>
>
> A  <- matrix(1:54, ncol=6)      # my original matrix
> A.new <- matrix(nrow=3, ncol=6) # a new matrix which I'll fill
> # for-loop
> for(i in 1:nrow(A.new)){
>     B <- A[i:(i+2), ]   # selecting the rows
>     C <- apply(B,2,sum) # summing by columns
>     A.new[i,] <- C      # inserting in the new matrix
> }
>
>
> +++++
> This mail has been sent through the MPI for Demographic
> Rese...{{dropped}}
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html