I am having some piece of R code where i need to multiply the elements that are not even in the matrix by two times
set.seed(75)
M <- matrix(sample(30, replace=T), nrow=5, ncol=5)
Fun4 <- function(M){
for (i in 1:nrow(M)){
for (j in 1:ncol(M)){
if(M[i][j]%%2!=0){
M[i][j] <- 2*M[i][j]
}
}
}
Res <- Fun4(M)
print(Res)
In a matrix with random numbers, we want to multiply only the odd numbers by two, and then print our new matrix
when running the code i am hiving the error as :
Error in if(M[i][j]%%2!=0){:Missing value where True / False Needed