-1

I have 5 (old) matrices of dim 1000*35. I want to create 1000 new matrices of dim 5*35 so that the first matrix is created from the first rows of the 5 old matrices. The second matrix is created from the second rows of the 5 old matrices and so on.

How can I do it? Please guide me.

baudsp
  • 4,076
  • 1
  • 17
  • 35
DR KASHK
  • 3
  • 3
  • 2
    Put your original matrices in a list. See [this post](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for details. It discusses data.frames, but the process is identical for matrices. Next, run `newList <- unlist(lapply(myList, function(x) split(x, row(x))), recursive=FALSE)`. Now, you have a list of length 1000, with your matrices. For more help, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – lmo Oct 27 '17 at 12:46

4 Answers4

1

First cbind all five old matrices (call them m1, ..., m5) and then create a new matrix for each row with apply

new.matrices <- apply(cbind(m1, m2, m3, m4, m5), 1, function(row) matrix(row, 5, 35))

This gives you a list of new matrices.

storaged
  • 1,837
  • 20
  • 34
0

To use the example posted here, rbind all your matrices, and perform the following:

 A=rbind(matrix1, matrix2, matrix3, matrix4, matrix5)
 lapply(0:(nrow(A)/5-1),function(x)A[1:5+5*x,])

This will be much quicker than splitting your data.

Onyambu
  • 67,392
  • 3
  • 24
  • 53
0

First of all, here's how I parsed the question:

Problem: turn {n matrices of i rows and j columns} into {i matrices of n rows and j columns}

Constraint: row x of new matrix y equals row y in old matrix x

Below is my suggested, generalized, solution (with lower dimensions than in the presented question, for the sake of presentation). The result is the same as @ANG's answer, but we can avoid writing out the for loop, and have to do less manual work writing out the original matrices.

n <- 3  # number of old matrices
i <- 5  # number of rows in old matrices
j <- 4  # number of columns in old matrices

# Create a list containing the original matrices.
old_matrices <- lapply(seq_len(n), matrix, nrow = i, ncol = j)

# Paste all the original matrices together by column.
m <- do.call(cbind, old_matrices)

Each row of the big matrix m now represents a single new matrix, with its rows concatenated together to form a vector.

# Construct the new matrices from the rows in the big matrix.
new_matrices <- tapply(m, row(m), matrix, nrow = n, byrow = T)

And then some checks to make sure we did what we were supposed to:

new_matrices[[1]]
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    2    2    2    2
#> [3,]    3    3    3    3

# Check that there's the right number of new matrices
length(new_matrices) == i
#> [1] TRUE

# Check that the new matrices all have the correct dimensions
all(vapply(new_matrices, function(m) all.equal(dim(m), c(n, j)), logical(1)))
#> [1] TRUE

# Check that the constraint holds
all(vapply(seq_along(new_matrices), function(y) {
  vapply(seq_along(old_matrices), function(x) {
    all.equal(new_matrices[[y]][x, ], old_matrices[[x]][y, ])
  }, logical(1))
}, logical(n)))
#> [1] TRUE
Mikko Marttila
  • 10,972
  • 18
  • 31
-1

You can also use a for() loop. Here is a detailed example

# old matrices
old.mat1 <- matrix(data = sample(35000), nrow = 1000, ncol = 35)
old.mat2 <- matrix(data = sample(35000), nrow = 1000, ncol = 35)
old.mat3 <- matrix(data = sample(35000), nrow = 1000, ncol = 35)
old.mat4 <- matrix(data = sample(35000), nrow = 1000, ncol = 35)
old.mat5 <- matrix(data = sample(35000), nrow = 1000, ncol = 35)

rbind(old.mat1[1, ], old.mat2[1, ], old.mat3[1, ], old.mat4[1, ],
      old.mat5[1, ]) # how we will build new matrices
dim(rbind(old.mat1[1, ], old.mat2[1, ], old.mat3[1, ], old.mat4[1, ],
          old.mat5[1, ])) # you can see that dim is 5*35

new.mat <- list() # list of new matrices
for(i in 1:1000){
        new.mat[[i]] <- rbind(old.mat1[i, ], old.mat2[i, ], old.mat3[i, ],
                              old.mat4[i, ], old.mat5[i, ])
}

new.mat1 <- new.mat[[1]] # first new matrix
length(new.mat) # new.mat contains the 1000 new matrices
nghauran
  • 6,648
  • 2
  • 20
  • 29