Question

I am trying to use a two dimension matrix to produce a two dimension matrix result where the number of rows and number of columns are determined in a new way everytime I change the values in the function which determines the number of rows and number of columns accordingly. The function that I would like to ask and resolve the "subscript out of bounds" problem is the following:

 HRC <- function(n,b,c)
 { 
 R=matrix( ,nrow = n*b, ncol = c)
 R[0,]=133
 for (j in 1:c)
 {
 r=rnorm(n*b)
 for (i in 1:n*b){
 R[i+1,j]=R[i,j]+3*b/r[i]
 }
 }
 return(R)
 }

 HRC(10,1,3)

The error message that I get is the following:

 Error in R[i + 1, j] = R[i, j] + 3 * b/r[i] : subscript out of bounds

I wonder how I can resolve this problem. Thank you so much in advance.

Was it helpful?

Solution 2

The problem is that you loop from row b to row n*b (with stride b, due to the precedence of * and :) and then index to one greater, so you attempt to index row n*b + 1 of R, which is out of bounds.

R[0,]<- will cause incorrect results but not elicit an error from R.

I find the code easier to read if you loop from 2 to n*b, the number of rows, and write the formula in terms of creating row i from row i-1 (rather than creating row i+1 from row i).

In addition, you can drop one loop dimension by vectorizing the operations over the rows:

HRC <- function(n, b, c) { 
  R <- matrix(NA, nrow = n*b, ncol = c)
  R[1,] <- 133

  r <- matrix(rnorm(n*b*c), ncol=c)
  for (i in 2:(n*b)){
    R[i,] <- R[i-1,] + 3*b/r[i-1,]
  }

  return(R)
}


HRC(10,1,3)

Here, the same number of random samples are taken with rnorm but they are formed as a matrix, and used in the same order as used in the question. Note that not all of the random values are actually used in the computation.

If you set a random seed and then run this function, and the function in @flodel's answer, you will get identical results. His answer is also correct.

OTHER TIPS

R's indexing starts at 1, not 0.

You also have to be careful with the operators precedence rules: the : operator has higher precedence than *. See ?Syntax.

This should work:

HRC <- function(n, b, c) { 
  R <- matrix(NA, nrow = n*b, ncol = c)
  R[1,]=133
  for (j in 1:c) {
    r = rnorm(n*b)
    for (i in 1:(n*b-1)){
      R[i+1,j] = R[i,j] + 3*b/r[i]
    }
  }
  return(R)
}

HRC(10,1,3)

I think you are making three mistakes:

First: You are messing up the row count on the index. It should be 1:(n*b) and not 1:n*b.

Second: In R, indexing starts at 1. So R[0,] should be replaced by R[1,].

Third: You are running the loops in the right bounds 1:c and 1:(n:b), but you are probably not keeping track of the indices.

Try this:

set.seed(100)
HRC <- function(n, b, c) { 
  R <- matrix(0, nrow = n*b, ncol = c)
  R[1,] <- 133
  for (j in 1:c) {
    r <- rnorm(n*b)
    for (i in 2:(n*b)){
      R[i,j] <- R[i-1,j] + 3*b/r[i-1]
    }
  }
  return(R)
}

HRC(10,1,3)

Lastly, I would like to warn you about interchangeable use of the assignment operators. See here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top