I would like to make a cumsum of multiple matrix obtaining the steps. If we consider:
A <- structure(c(1, 2, 3, 2, 3, 1, 4, 1, 2), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 1 2 4
# [2,] 2 3 1
# [3,] 3 1 2
B <- structure(c(6, 1, 9, 6, 3, 7, 3, 2, 8), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 6 6 3
# [2,] 1 3 2
# [3,] 9 7 8
C <- structure(c(1, 1, 2, 5, 3, 3, 3, 9, 1), .Dim = c(3, 3))
# [,1] [,2] [,3]
# [1,] 1 5 3
# [2,] 1 3 9
# [3,] 2 3 1
I would like the following results:
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 2 3 1
[3,] 3 1 2
[,1] [,2] [,3]
[1,] 7 8 7
[2,] 3 6 3
[3,] 12 8 10
[,1] [,2] [,3]
[1,] 8 13 10
[2,] 4 9 12
[3,] 14 11 11
with all steps! I could do this with a for loop, but it's slow with big matrix, how can I do this with apply ?
This is a perfect job for Reduce:
Reduce("+", list(A,B,C), accumulate=TRUE)
[[1]]
[,1] [,2] [,3]
[1,] 1 2 4
[2,] 2 3 1
[3,] 3 1 2
[[2]]
[,1] [,2] [,3]
[1,] 7 8 7
[2,] 3 6 3
[3,] 12 8 10
[[3]]
[,1] [,2] [,3]
[1,] 8 13 10
[2,] 4 9 12
[3,] 14 11 11
Related
Suppose I have a list of nine, 2 x 2 matrices as defined by:
mat_list <- list(matrix(1, 2, 2), matrix(2, 2, 2), matrix(3, 2, 2),
matrix(4, 2, 2), matrix(5, 2, 2), matrix(6, 2, 2),
matrix(7, 2, 2), matrix(8, 2, 2), matrix(9, 2, 2))
I would like to merge these matrices into a single 6 x 6 matrix. It would look like this:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 4 4 7 7
[2,] 1 1 4 4 7 7
[3,] 2 2 5 5 8 8
[4,] 2 2 5 5 8 8
[5,] 3 3 6 6 9 9
[6,] 3 3 6 6 9 9
I can accomplish this task using the following code:
do.call( cbind, list( do.call( rbind, mat_list[1:3]),
do.call( rbind, mat_list[4:6]),
do.call( rbind, mat_list[7:9])) )
But how can this be generalized for a very large list of matrices? It would be too tedious to write out the list of do.call functions.
Maybe we can do like this
do.call(
cbind,
lapply(
split(mat_list, ceiling(seq_along(mat_list) / 3)),
function(x) do.call(rbind, x)
)
)
which gives
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 4 4 7 7
[2,] 1 1 4 4 7 7
[3,] 2 2 5 5 8 8
[4,] 2 2 5 5 8 8
[5,] 3 3 6 6 9 9
[6,] 3 3 6 6 9 9
or
> do.call(cbind, Map(function(x) do.call(rbind, x), data.frame(matrix(mat_list, 3, 3))))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 4 4 7 7
[2,] 1 1 4 4 7 7
[3,] 2 2 5 5 8 8
[4,] 2 2 5 5 8 8
[5,] 3 3 6 6 9 9
[6,] 3 3 6 6 9 9
Suppose if I have a matrix with dimension 87x2. How can I convert into the dimension 29x6 in r
set.seed(1)
mat1 = matrix(runif(174), 87, 2)
I wanted to have like this below
> matrix(c(1:12), 6, 2)
[,1] [,2]
[1,] 1 7
[2,] 2 8
[3,] 3 9
[4,] 4 10
[5,] 5 11
[6,] 6 12
> matrix(c(1:12), 2, 6)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 3 5 7 9 11
[2,] 2 4 6 8 10 12
Thank you in advance.
You can do the following:
mat1 <- matrix(c(1:12), 6, 2)
matrix(mat1, nrow = 2, ncol = 6)
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 3 5 7 9 11
#[2,] 2 4 6 8 10 12
Or set the dimensions directly using dim
dim(mat1) <- c(2, 6)
I want to sample 'n' rows from a matrix:
data <- matrix(data = 1:12, nrow = 4, ncol = 3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
I use the following code :
selection <-sample(nrow(data),size = 2, replace = FALSE)
data[selection,]
[,1] [,2] [,3]
[1,] 4 8 12
[2,] 3 7 11
Is there a way I could return a matrix containing only the rows that haven't been sampled? In this case:
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
Many thanks in advance.
I already have values of e1,e2,e3,e4,e5,e6 then how could i Put it into a 3x3 matrix in the following form in RStudio :
[1] [2] [3]
[1] e1 e4 e5
[2] e4 e2 e6
[3] e5 e6 e3
Fill in the off-diagonals, then fill in the diagonal:
x <- 1:6
out <- matrix(nrow=3,ncol=3)
out[c(which(lower.tri(out)), which(upper.tri(out)))] <- tail(x,3)
diag(out) <- head(x,3)
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 4 2 6
#[3,] 5 6 3
vec2mat1=function(vec){
l=length(vec)
p=round(uniroot(function(x)x*(x+1)/2-l,c(0,1))[[1]],0)
a=diag(vec[1:p],p)
a[lower.tri(a)]=tail(vec,-p)
a[upper.tri(a)]=t(a)[upper.tri(a)]
a
}
vec2mat1(1:6)
[,1] [,2] [,3]
[1,] 1 4 5
[2,] 4 2 6
[3,] 5 6 3
vec2mat1(1:10)
[,1] [,2] [,3] [,4]
[1,] 1 5 6 7
[2,] 5 2 8 9
[3,] 6 8 3 10
[4,] 7 9 10 4
The syntax is column-wise
matrix(c(1, 4, 5,
4, 2, 6,
5, 6, 3), ncol=3)
# [,1] [,2] [,3]
# [1,] 1 4 5
# [2,] 4 2 6
# [3,] 5 6 3
I have a vector v, and I would like to create the following matrix. How can I do this in R?
v = c(1, 2, 3, 4)
> m = matrix(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,4), nrow=4)
> m
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 1 2 3 4
[3,] 1 2 3 4
[4,] 1 2 3 4
See ?matrix and the nrow, ncol, byrow arguments:
matrix(v, nrow=4, ncol=4, byrow=TRUE)
# [,1] [,2] [,3] [,4]
#[1,] 1 2 3 4
#[2,] 1 2 3 4
#[3,] 1 2 3 4
#[4,] 1 2 3 4