Link to home
Start Free TrialLog in
Avatar of camper12
camper12

asked on

R program loop

Hi,

The following code gives me an error:
Error: unexpected '{' in:
"for(i in 1 : nrow(VarM)) {
  for (j in 1 : ncol(VarM) {"
>     CorrM[i,j] = VarM[i,j]/(SDM[i,1]*SDM[j,1])
Error: object 'i' not found
>   }
Error: unexpected '}' in "  }"
> }
Error: unexpected '}' in "}"


Code:
VarM = matrix(c(.0016,.001,.0015,.001,.002,.0019,.0015,.0019,.0042),nrow=3,byrow = TRUE)
A = VarM[1,1]
SD_A = sqrt(A)
B = VarM[2,2]
SD_B = sqrt(B)
C = VarM[3,3]
SD_C = sqrt(C)
SDM = matrix(c(SD_A,SD_B,SD_C),nrow=3,byrow=TRUE)
SDM
CorrM=matrix(data=NA, nrow=3, ncol=3, byrow=TRUE)
for(i in 1 : nrow(VarM)) {
  for (j in 1 : ncol(VarM) {
    CorrM[i,j] = VarM[i,j]/(SDM[i,1]*SDM[j,1])
  }
}

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Brendan M
Brendan M
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial