Resources

error G0058 : index out of range when indexcat cannot find a match

0

I have some code that tries to find the index of an element in a vector. It then tries to assign the matches to another variable. Here is a working example:

x = { -1, 0, 1, 2, 3, 4 };
idx = indexcat(x, (x < 0));
z = x[idx];

This assigns all members of x that are less than zero to z. In this case it is only -1. If there are no matches, however, it will fail with error G0058 : index out of range. For example:

x = { 1, 2, 3, 4 };
idx = indexcat(x, (x < 0));
z = x[idx];

Is there a way for me to catch this before the error ends my program?

Tags: asked November 26, 2013
aptech
342

1 Answer

0

If the function indexcat cannot find a match, it will return a scalar error code. Scalar error codes in GAUSS are very much like a missing value that contains an integer error code.

For your purposes, you will just need to check the output from indexcat to see if it is something that will not work as a valid index. The GAUSS function isinfnanmiss is probably what you want to use. This function will return a 1 if the input is an infinity, a Nan or a missing value.

Here is how you could use it in your code:

x = { 1, 2, 3, 4 };
idx = indexcat(x, (x < 0));

//check to see if 'idx' contains an invalid index value
if isinfnanmiss(idx);
   //code to handle case where match
   //was not found in x
else;
   z = x[idx];
endif;
aptech
342