Hi!
I'm new to mata and I'm trying to populate elements of an empty matrix (depending whether they are diagonal elements or not) conditional on values of another matrix.
The way I coded this is:
W=(1,2\1,2\2,2)
R=J(3,3,0)
for (i=1; i<=rows(R); i++) {
for (j=1; j<=cols(R); j++) {
for (k=1; k<=rows(W); k++) {
if (i==j && W[k,1]==W[k,2])
R[i,j]=W[k,1]+W[k,2]
else if (i==j && W[k,1]!=W[k,2])
R[i,j]=1
else
R[i,j]=0
}
}
}
I've attached the output matrix R, which shows that it doesn't work. Can anyone explain why this is the case?
Kind regards,
Ulrich
I'm new to mata and I'm trying to populate elements of an empty matrix (depending whether they are diagonal elements or not) conditional on values of another matrix.
The way I coded this is:
W=(1,2\1,2\2,2)
R=J(3,3,0)
for (i=1; i<=rows(R); i++) {
for (j=1; j<=cols(R); j++) {
for (k=1; k<=rows(W); k++) {
if (i==j && W[k,1]==W[k,2])
R[i,j]=W[k,1]+W[k,2]
else if (i==j && W[k,1]!=W[k,2])
R[i,j]=1
else
R[i,j]=0
}
}
}
I've attached the output matrix R, which shows that it doesn't work. Can anyone explain why this is the case?
Kind regards,
Ulrich
Comment