Error array subscript is not an integer là gì năm 2024

This error occurs when you attempt to index into an array using indices that are not positive integers or logical values. Here are some tips for common situations that cause this error message:

  1. Double check that your indices are positive integers. Indices in MATLAB cannot be 0, and by default, start with 1.
  1. If you are using logical indexing to index into an array, be sure that your index array is of type 'logical', and not a 'double' array of 1s and 0s. You can convert a 'double' array to a logical array before attempting to use logical indexing. For example:

A = [1 2 3 4; 5 6 7 8];

ind_double = [0 1 0 1; 0 1 0 1];

ind_logical = logical(ind_double);

A(ind_logical)

For an index array 'ind', you can check its data type using the 'whos' function:

  1. If you use floating-point arithmetic to compute an index array, then the array values may not be exact integers. The 'round' function is handy when you know you have an index value that is nearly the integer index that you want. For example,

A = [1 2 3 4; 5 6 7 8];

ind_float = 2.00001;

ind_int = round(ind_float);

A(ind_float)

Below is a way to check if an index array 'ind'' contains exact integer values. This command returns a 'logical' array, where 1 indicates the index value is an exact integer, and 0 indicates it is not.

  1. If you assign a variable to the same name as a built-in function in MATLAB, then you will overwrite that function and encounter the error when you attempt to call it. For example,

max = rand(5);

A = rand(5);

max(A)

In this event, rename your variable and clear the old one to proceed:

B = max;

clear max max(A)

For more information on indexing in MATLAB, see the following documentation page:

Más respuestas (2)

Error array subscript is not an integer là gì năm 2024

Iaredi Sabinas's comment should be a valid answer.

It may happen when you have a variable named after an existing matlab function, like min or diff. Matlab thinks you are using the variable instead of the builtin function.

You can reference an array element with a variable. You can not define an array with a variable. You either explicitly state a size or if using a list of variables do not define a size but let the number of elements in the list define the size.

Post log and code entries into code windows opened on the forum with the {I} or "running man" icons to preserve formatting.

The error is fairly clear:

365 data work.test1; 366 set work.test; 367 array test{c} a-- c;

                     -
                     22
                     202
ERROR: Too many variables defined for the dimension(s) specified for the array test. ERROR 22-322: Syntax error, expecting one of the following: an integer constant, *. ERROR 202-322: The option or parameter is not recognized and will be ignored. 368 run;

The underlined C and error 322 is telling that you need either an actual integer, i.e.3 or *

the Code should be

data work.test1;

 set work.test;
 array test{*} a-- c;
run;

Using a VARIABLE integer to reference elements of an array:

data work.test1;

 set work.test;
 array test{*} a-- c;
 do i = 1 to dim(test);
  x= test[i];
  put _n_= I= x=;
end;
run;

The variable I takes on values of 1 , 2 and 3. _n_ is an automatic variable that indicates, in this case, which record has been read from the input set.

Arrays can only be indexed using values that have integral type (char, int, long, unsigned, etc). They cannot be indexed using non-integral values (float as in your case, struct types, etc).

There is also an obscure corner of C that causes "array[index]" and "index[array]" to be equivalent (assuming array is (well....) an array or a pointer, and index has integral type). Because of that, your expression "array[last[array]]" is equivalent to "array[array[last]]" which attempts to use array[last] as an index. In your code, array[last] is of type float so is not a valid index. Hence the compiler error.

Right 98% of the time, and don't care about the other 3%. If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  • 11-06-2012
    Error array subscript is not an integer là gì năm 2024
    SAMARAS
    Error array subscript is not an integer là gì năm 2024
    ---
    What i thought you wanted to do

Code:

include

int main( int argc, char *argv[]) { float array[5]; int last = 4; array[0]=1.2; array[1]=1.3; array[2]=1.4; array[3]=1.5; array[4]=1.9; printf("%f\n",array[last]); return(0); }

grumpy is more experienced,so he is sure to have it better in his mind
Error array subscript is not an integer là gì năm 2024
---

11-06-2012

Error array subscript is not an integer là gì năm 2024

Algorithm Dissector

Error array subscript is not an integer là gì năm 2024


You get rid of the error by fixing or removing the nonsense code. Explain what you really want to do and we'll show you how to write code for it that makes sense.

My homepage Advice: Take only as directed - If symptoms persist, please see your debugger

Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"