Skip to content

Repeat columns and rows of matrix

5 messages · az14, Jean-Daniel Sylvain, Roger Bivand +1 more

#
Dear list, 
i have a matrixW(1060*1060), i want to repeat each column and row 9 times,
such as to have a new matrix W1(9540*9540).
I use the code: 
rep.row<-function(x,n){
   matrix(rep(x,each=n),nrow=n)
}
rep.col<-function(x,n){
   matrix(rep(x,each=n), ncol=n, byrow=TRUE)
}

ww<-rep.row(w,9)
W1<-rep.col(ww,9)

but i did not work! 

Is there another way to do it ?

Any help would me appreciated. 
Thank YOU.



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Repeat-columns-and-rows-of-matrix-tp7586408.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
#
Hi,

You hould use the apply function function, which will apply the function 
rep() on one of the dimension of your matrix (1 for row, and 2 for columns).

mat <- matrix(data=1:100,10,10)

apply(mat,MARGIN=1,function(x) rep(x,10))
apply(mat,MARGIN=2,function(x) rep(x,10))

Hope this helps,

JD

Le 5/9/2014 7:33 AM, az14 a ?crit :
#
If this question is related to an earlier one (LM tests for spatial panel 
data), I don't think that the correct expansion of W for T=2 is:

W W
W W

but rather

W 0
0 W

so a Kronecker product of W and diag(T). Here the output is sparse, and 
with T=9 and N=1K, this may be important, especially if W is sparse too. 
However, we don't know. So for now we need

M <- matrix(1, nrow=9, ncol=9)
W9 <- kronecker(W, M)

which answers the apparent question. However, I don't think that it was 
the intention.

Roger
On Fri, 9 May 2014, Jean-Daniel Sylvain wrote:

            

  
    
#
Hi, acctually it's for the (LM tests for spatial panel data).
w is a distance matrix ( wih cut of 70km) . 
M <- matrix(1, nrow=9, ncol=9)
W9 <- kronecker(W, M)
I get this error: 

Erreur dans `dimnames<-.data.frame`(`*tmp*`, value = list(n)) : 
  invalid 'dimnames' given for data frame




--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Repeat-columns-and-rows-of-matrix-tp7586408p7586411.html
Sent from the R-sig-geo mailing list archive at Nabble.com.