Skip to contents

Assemble several matrices of same dimensions by performing by case operation. For instance, with kind.of.operation = "+", the function will add the value of all the case 1 (row1 & column1) of the matrices and put it in the case 1 of a new matrix M, according to the formula

$$c_{ij} = \sum_{k=1}^{k=z}c_{ijk}$$ c: case

i: row number

j: column number

k: matrix number

z: number of matrices

Usage

mat_op(mat.list, kind.of.operation = "+", safer_check = TRUE)

Arguments

mat.list

List of matrices.

kind.of.operation

Either "+" (by case addition), "-" (by case subtraction) or "*" (by case multiplication).

safer_check

Single logical value. Perform some "safer" checks (see https://github.com/safer-r)? If TRUE, checkings are performed before main code running: 1) R classical operators (like "<-") not overwritten by another package because of the R scope and 2) required functions and related packages effectively present in local R lybraries. Set to FALSE if this fonction is used inside another "safer" function to avoid pointless multiple checkings.

Value

The assembled matrix, with row and/or column names only if all the matrices have identical row/column names.

Author

Gael Millot <gael.millot@pasteur.fr>

Yushi Han <yushi.han2000@gmail.com>

Haiding Wang <wanghaiding442@gmail.com>

Examples

mat1 = matrix(c(1,1,1,2,1,5,9,8), ncol = 2) ; 
mat2 = matrix(c(1,1,1,2,1,5,9,NA), ncol = 2) ; 
mat_op(mat.list = list(mat1, mat2), kind.of.operation = "+")
#>      [,1] [,2]
#> [1,]    2    2
#> [2,]    2   10
#> [3,]    2   18
#> [4,]    4   NA