create a loop to get matrices convert them to numeric and then add them together in r

# Create a list of matrices
matrices <- list(matrix(1:4, nrow=2), matrix(5:8, nrow=2), matrix(9:12, nrow=2))

# Initialize a variable to store the sum of matrices
sum_matrix <- matrix(0, nrow=2, ncol=2)

# Loop through each matrix in the list
for(mat in matrices) {
  mat_numeric <- as.numeric(mat)  # Convert matrix to numeric
  sum_matrix <- sum_matrix + mat_numeric  # Add numeric matrix to sum_matrix
}

print(sum_matrix)
426 chars
14 lines

related categories

gistlibby LogSnag