Skip to content

splitting matrices

6 messages · Eliza Botto, MacQueen, Don, arun +2 more

#
This example illustrates a method that may do what you want.

## split a 20x5 matrix into five 4x5 matrices
xmat <- matrix(1:100, nrow=20, ncol=5, byrow=TRUE)
xsplit <- rep( 1:5, times= rep(4,5))
tmp <- split.data.frame(xmat,xsplit)

You will need to change xsplit to fit your dimensions.

-Don
#
Hi,
You could also try:
set.seed(51)
mat1<-matrix(sample(1:5000,1116*12,replace=TRUE),nrow=1116)
?dim(mat1)
#[1] 1116?? 12

res1<-lapply(split(as.data.frame(mat1),rep(1:36,each=31)),as.matrix)
?unlist(lapply(res1,nrow))
# 1? 2? 3? 4? 5? 6? 7? 8? 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
#31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 
#27 28 29 30 31 32 33 34 35 36 
#31 31 31 31 31 31 31 31 31 31 

A.K.




----- Original Message -----
From: "MacQueen, Don" <macqueen1 at llnl.gov>
To: eliza botto <eliza_botto at hotmail.com>; "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Thursday, January 3, 2013 11:56 AM
Subject: Re: [R] splitting matrices

This example illustrates a method that may do what you want.

## split a 20x5 matrix into five 4x5 matrices
xmat <- matrix(1:100, nrow=20, ncol=5, byrow=TRUE)
xsplit <- rep( 1:5, times= rep(4,5))
tmp <- split.data.frame(xmat,xsplit)

You will need to change xsplit to fit your dimensions.

-Don
#
Alternatively you could use

dim(dd) <- c(31, 36, 12)

That will give you an array in which each small matrix is defined as

dd[,1:36,]

You can work with that directly, or if you want the separate matrices to be
represented by the third index:

ddd <- aperm(dd, c(1, 3, 2))

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352