in or out of a for loop in R - calculating the diagonal product of a matrix -
i'm trying find maximum diagonal product of 2 digit numbers in 20x20 matrix.
this gives error message :
i <- 17:1 z <- (j in 1:(18-i)) {b <- max ((x[i,j]*x[i+1,j+1]*x[i+2,j+2]*x[i+3,j+3]))}}
but doesn't:
z <- (i <- 17:1) {for (j in 1:(18-i)) {b <- max ((x[i,j]*x[i+1,j+1]*x[i+2,j+2]*x[i+3,j+3]))}}
but second version gives me number small . why first 1 not work, think yield correct answer, don't understand error message.
this looks wrong.
you cannot assign result of for
loop variable. , max()
on scalar variable nonsense. lastly, matrix x
isn't specified. i'd retry smaller, , maybe print interim results screen.
walk before run still advice. later can still vectorise sprint solution.
Comments
Post a Comment