0

I don't receive correct results out of nested for loops in R (version 2.15.2).

GCP <- matrix(nrow=143,ncol=4)
for (i in 1:13) {
 for (j in 1:11) {
 GCP[i*j,1] = 410*(j-1)
 GCP[i*j,2] = 400*(i-1)
 # print(GCP[i*j,1])
 }
}

When I uncomment print(GCP[i*j,1]) I receive just what I expect:

[1] 0
[1] 410
[1] 820
[1] 1230
[1] 1640
[1] 2050
[1] 2460
[1] 2870
[1] 3280
[1] 3690
[1] 4100
[1] 0
[1] 410
[1] 820
[1] 1230
[1] 1640
[1] 2050
[1] 2460
[1] 2870
[1] 3280
[1] 3690
[1] 4100
[1] 0
...

But when I enter GCP[,1] after the loops have finished:

  [1]    0    0    0    0    0    0    0    0    0    0    0    0    0  410  820
 [16]  410   NA  410   NA  410  820  410   NA  410 1640  410  820 1230   NA  820
 [31]   NA 1230  820   NA 1640  820   NA   NA  820 1230   NA 2050   NA 1230 1640
 [46]   NA   NA 1230 2460 1640   NA 1230   NA 2050 1640 2460   NA   NA   NA 1640
 [61]   NA   NA 2460 2870 1640 2050   NA   NA   NA 2460   NA 2050   NA   NA   NA
 [76]   NA 2460 2050   NA 2870 3280   NA   NA 2460   NA   NA   NA 2870   NA 3280
 [91] 2460   NA   NA   NA   NA 2870   NA   NA 3280 3690   NA   NA   NA 2870   NA
[106]   NA   NA 3280   NA 3690   NA   NA   NA   NA   NA   NA 3280   NA   NA 3690
[121] 4100   NA   NA   NA   NA   NA   NA   NA   NA 3690   NA 4100   NA   NA   NA
[136]   NA   NA   NA   NA   NA   NA   NA 4100

How should I correct my code ?

3
  • 4
    Can you clarify what are you trying to do? Commented Feb 3, 2013 at 11:31
  • When i==1 and j==2 GCP[2,1] is set to 410 and printed. But when i==2 and j==1 GCP[2,1] is overwritten with 0 and printed again. Is it your intention? Commented Feb 3, 2013 at 11:47
  • You are right concerning rewriting the values, thank you very much! What I intend to do is to fill the GCP[,1] and GCP[,2] just as inserted print() output. Commented Feb 3, 2013 at 13:25

1 Answer 1

2

The problem is that, when you do:

GCP <- matrix(nrow = 143, ncol = 4)

you are essentially initalising a matrix of dimensions (143,4) with NA. Then, you are constructing values only for row = i * j. And you are only printing those values. Maybe this code will help you clear things up a bit:

GCP <- matrix(nrow = 143, ncol = 4)
for (i in 1:13) {
    for (j in 1:11) {
        GCP[i*j,1] = 410*(j-1)
        GCP[i*j,2] = 400*(i-1)
        cat("i =", i, "j =", j, "i*j =", i*j, "GCP[i*j] = ", GCP[i*j,1], "\n")
    }
}

You'll see that the loop runs through all values of i and j. However, your indexes are only filling certain values = i*j. So, the remaining values are still NA. Since you are printing only those values that are already set, you never get NA when you print it.

I can't offer a solution unless you explain what you are trying to do. To start with, you are setting a matrix of ncol=4 but you are filling only the first two columns, and as @redmode points out, you ar rewriting some values.

Edit: Maybe then, this is what you're looking for?

GCP <- matrix(nrow = 143, ncol = 4)
for (i in 1:13) {
    for (j in 1:11) {
        GCP[11*(i-1) + j, 1] = 410*(j-1)
        GCP[11*(i-1) + j, 2] = 400*(i-1)
    }
}

This goes through the values from 1 to 143.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer, I really intend to fill only the GCP[,1] and GCP[,2] with the values that inserted print() output.
@user2036954, please check the edit. Is this what you wanted to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.