0

Supposed that I have 99 matrix with same column and row length, and try to sum them up (element by element).

99 matrix are already assigned.

What I want to make is the code of something like below.

var <- sprintf("S%02d", 1:99)  # seq. of matrix names
SUM_S <- sum(var)              # S01 + S02 + ... + S99

please give me an idea of summing up many matrix in a short code.

KENgerine
  • 33
  • 2
  • Sadly Richard Scriven has deleted his comment, despite the fact that it was correct and my answer was not. Hopefully he will compose an answer instead. – IRTFM Mar 14 '16 at 04:43
  • http://stackoverflow.com/questions/11641701/sum-a-list-of-matrices – Sotos Mar 14 '16 at 07:46

1 Answers1

0

You can use Reduce to apply a binary function over a list.

Something like Reduce("+", lapply(var, function(x) eval(as.name(x)))) , where the lapply part is to create a list of all the matrices from the list of variable names in var.

Ricky
  • 4,616
  • 6
  • 42
  • 72