matrix out of range

Hi. when running my code I always get this error message matrix out of range. I know what it means but I do not know how to correct it.

3 Answers



1



The first step to resolve this problem is to find out what the values are when the index is out of range. You can either use the GAUSS debugger, or you can use print statements to find this out. Here is a very, very simple example of using print statements to find an out of range problem:

//Create a 5 x 1 matrix of zeros
x = zeros(5, 1);

//Loop 10 times, setting the ith element
//of 'x' to be equal to 'i'
for i(1, 10, 1);
   //Add diagnostic print statements
   print "i = " i;
   print "number of rows in x = " rows(x);
   x[i] = i;
endfor;

The code above will print the following output:

i =        1
number of rows in x =        5
i =        2 
number of rows in x =        5
i =        3 
number of rows in x =        5
i =        4 
number of rows in x =        5
i =        5 
number of rows in x =        5
i =        6 
number of rows in x =        5

which shows us that the program ends, when it tries to assign to the sixth element of a vector that only has five elements.

aptech

1,773


0



Dear

Thank for your prompt response, I did run the above code you gave me and I have exactly he same output and it said at the end "index out of range " line 10 which is actually

x[i] = i.

now how should I solve my problem ? I can send you the code I am trying to run with the data!!
 

emi

0


0



The example code that I posted was just to show the process of finding the problem. In the code that I posted, our last line of printing says:

i =        6 
number of rows in x =        5

Since the next line of code is:

x[i] = i;

we can see that the code is trying to place the value of i into the sixth element of x. However, x only has five elements--it does not have a sixth element, so the code fails with an "index out of range error".

To resolve the error in this code, we need to either make x larger before the loop so it has enough elements to hold the output from each iteration, or we need to decrease the number of iterations that the loop runs.

In your code there will most likely be a very similar situation with a similar resolution.

aptech

1,773

Your Answer

3 Answers

1

The first step to resolve this problem is to find out what the values are when the index is out of range. You can either use the GAUSS debugger, or you can use print statements to find this out. Here is a very, very simple example of using print statements to find an out of range problem:

//Create a 5 x 1 matrix of zeros
x = zeros(5, 1);

//Loop 10 times, setting the ith element
//of 'x' to be equal to 'i'
for i(1, 10, 1);
   //Add diagnostic print statements
   print "i = " i;
   print "number of rows in x = " rows(x);
   x[i] = i;
endfor;

The code above will print the following output:

i =        1
number of rows in x =        5
i =        2 
number of rows in x =        5
i =        3 
number of rows in x =        5
i =        4 
number of rows in x =        5
i =        5 
number of rows in x =        5
i =        6 
number of rows in x =        5

which shows us that the program ends, when it tries to assign to the sixth element of a vector that only has five elements.

0

Dear

Thank for your prompt response, I did run the above code you gave me and I have exactly he same output and it said at the end "index out of range " line 10 which is actually

x[i] = i.

now how should I solve my problem ? I can send you the code I am trying to run with the data!!
 

0

The example code that I posted was just to show the process of finding the problem. In the code that I posted, our last line of printing says:

i =        6 
number of rows in x =        5

Since the next line of code is:

x[i] = i;

we can see that the code is trying to place the value of i into the sixth element of x. However, x only has five elements--it does not have a sixth element, so the code fails with an "index out of range error".

To resolve the error in this code, we need to either make x larger before the loop so it has enough elements to hold the output from each iteration, or we need to decrease the number of iterations that the loop runs.

In your code there will most likely be a very similar situation with a similar resolution.


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.