Pankaj
Pankaj

Reputation: 1336

Manipulation List of Matrices in R

I have a list of matrices, containing 100 matrices. L[[i]] is a square matrix of 200*200. I want to manipulate each matrix in D-L[[i]] format where D is a diagonal matrix with diagonal with diagonal as sum of L[[i]] over columns. I want something like

for(i in 1:100){ M_i = diag(as.matrix(rowSums(L[[i]]))) - as.matrix(L[[i]]) }

So that now I can find manipulated M_i and further want to print each M_i. Thanks in advance for any help in this direction.

Upvotes: 1

Views: 566

Answers (1)

akrun
akrun

Reputation: 887048

Try

lapply(L, function(x) diag(rowSums(x))-x)

data

set.seed(25)
L <- lapply(1:5, function(i) matrix(sample(1:20, 4*4,
         replace=TRUE), ncol=4))

Upvotes: 3

Related Questions