I have two matrices that i want to sum
mat1<-matrix(1:4,2,2)
mat2<-matrix(5:8,2,2)
mat1
[,1] [,2]
[1,] 1 3
[2,] 2 4
mat2
[,1] [,2]
[1,] 5 7
[2,] 6 8
what i want is this
mat_sum
[,1] [,2]
[1,] 6 10
[2,] 8 12
I tried
mat_sum <- sapply(seq_along(mat1), function(i)
mat1[[i]]+mat2[[i]])
but then it doesnt return a matrix
[1] 6 8 10 12
How can i get it to return a matrix?
We can do a regular + which will preserve the matrix format and does the elementwise summation
mat1 + mat2
# [,1] [,2]
#[1,] 6 10
#[2,] 8 12
If there are many matrices, place it in a list and use Reduce with +
Reduce(`+`, mget(paste0("mat", 1:2)))
Related
I have a question about adding up the column of the matrix
for example:
I have a matrix
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 4 2
I want it to be
[,1] [,2] [,3]
[1,] 1 4 5
[2,] 2 6 8
We can apply cumsum on each row by looping over the rows with apply and MARGIN specified as 1 and transpose the output
t(apply(m1, 1, cumsum))
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8
Or with a for loop
for(i in seq_len(ncol(m1))[-1]) m1[,i] <- m1[, i] + m1[, i-1]
Or another option is to split it a list of vectors with asplit and then Reduce with + and accumulate = TRUE
do.call(cbind, Reduce(`+`, asplit(m1, 2), accumulate = TRUE))
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8
or with a convenient function rowCumsums from matrixStats
library(matrixStats)
rowCumsums(m1)
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8
data
m1 <- cbind(1:2, 3:4, 1:2)
This question already has answers here:
Multiply rows of matrix by vector?
(6 answers)
Closed 1 year ago.
Suppose I have the following:
mat <- matrix(1:9, ncol = 3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Now I would like to multiply each column of the matrix by a scalar
scalar = c(1,2,3)
I would like the first element of scalar to multiply the first column, the second element multiply the second column and the third scalar the third column.
To obtain the following output
[,1] [,2] [,3]
[1,] 1 8 21
[2,] 2 10 24
[3,] 3 12 27
Could anyone help me with this?
Another base R option using %*% and diag
> mat %*% diag(scalar)
[,1] [,2] [,3]
[1,] 1 8 21
[2,] 2 10 24
[3,] 3 12 27
We replicate the 'scalar' and multiply
mat * scalar[col(mat)]
Or with sweep
sweep(mat, 2, scalar, `*`)
[,1] [,2] [,3]
[1,] 1 8 21
[2,] 2 10 24
[3,] 3 12 27
I have a question about adding up the column of the matrix
for example:
I have a matrix
[,1] [,2] [,3]
[1,] 1 3 1
[2,] 2 4 2
I want it to be
[,1] [,2] [,3]
[1,] 1 4 5
[2,] 2 6 8
We can apply cumsum on each row by looping over the rows with apply and MARGIN specified as 1 and transpose the output
t(apply(m1, 1, cumsum))
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8
Or with a for loop
for(i in seq_len(ncol(m1))[-1]) m1[,i] <- m1[, i] + m1[, i-1]
Or another option is to split it a list of vectors with asplit and then Reduce with + and accumulate = TRUE
do.call(cbind, Reduce(`+`, asplit(m1, 2), accumulate = TRUE))
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8
or with a convenient function rowCumsums from matrixStats
library(matrixStats)
rowCumsums(m1)
# [,1] [,2] [,3]
#[1,] 1 4 5
#[2,] 2 6 8
data
m1 <- cbind(1:2, 3:4, 1:2)
How can I shuffle the values of matrix m1 across each column:
Initial:
m1=cbind(c(1,2,3),c(4,5,6),c(7,8,9))
Do something and:
m1=cbind(c(7,5,3),c(4,2,9),c(1,8,6))
Thanks
You can call the sample function on each column of your matrix to shuffle it:
set.seed(100)
apply(m1, 2, sample)
# [,1] [,2] [,3]
# [1,] 1 5 8
# [2,] 3 4 9
# [3,] 2 6 7
ehm, you mean by row in your example?!
shuffle a list:
# create a list from 1 to 9
x <- seq(1,9)
# shuffle
x[order(runif(length(x)))]
shuffle rows/columns of a matrix:
# example matrix
m1 <- matrix(x,ncol=3)
# shuffle by row
for (i in 1:nrow(m1)) m1[i,] <- m1[i,order(runif(length(m1[i,])))]
# shuffle by col
for (i in 1:ncol(m1)) m1[,i] <- m1[order(runif(length(m1[i,]))),i]
edit: maybe sample is better... http://stat.ethz.ch/R-manual/R-devel/library/base/html/sample.html
You can also put sample in matrix indices and sample the rows and columns.
To shuffle the entire matrix,
> m1[sample(nrow(m1)), sample(ncol(m1))]
# [,1] [,2] [,3]
#[1,] 6 9 3
#[2,] 5 8 2
#[3,] 4 7 1
Or by row
> m1[sample(nrow(m1)), ]
# [,1] [,2] [,3]
#[1,] 3 6 9
#[2,] 1 4 7
#[3,] 2 5 8
Or by column
> m1[,sample(ncol(m1))]
# [,1] [,2] [,3]
#[1,] 7 4 1
#[2,] 8 5 2
#[3,] 9 6 3
Let M be the matrix:
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
[4,] 7 2
I would like to extract all rows with entries equal to the components of the vector
v <- c(3,6,1) from column [,1] in M producing the submatrix m:
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
I tried
m <- M[which(M[,1] == v), ]
Obtaining the error message longer object length is not a multiple of shorter object length.
Using the transpose t(v) of v does not help.
using %in%:
M[M[,1] %in% v,]
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4