Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:23:41+00:00 2026-06-18T08:23:41+00:00

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

  • 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:23:42+00:00Added an answer on June 18, 2026 at 8:23 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know a whole lot about streams. Why does the first version work using
Don't understand, if Data.Map is and [] is. I found this out while wondering
(Don't care about the version. IE or not IE.)
Don't freak out about the length of my post, it's pretty simple, I just
don't know if the title describes anything about what I'm trying to say but
Don't ask me how but I'm in a situation where I have DCPs published
don't know what happened in my jcrop selection. I think I pressed some keys
Don't be scared of the extensive code. The problem is general. I just provided
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't really know how to formulate the title, but it should be pretty obvious

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.