Home>Article> Array index must be a positive integer or logical value

Array index must be a positive integer or logical value

(*-*)浩
(*-*)浩 Original
2019-12-11 09:46:36 77995browse

Array index must be a positive integer or logical value

MATLAB When indexing an array, this error will occur if an index value that is not a positive integer or logical value is used.

The following are some suggestions when causing this error:## ) Double check that the index value is a positive integer. The index in MATLAB cannot be 0, and generally starts from 1.2) If you use logical variable index, please ensure that the index array type is a logical variable, not a double array composed of 1 and 0. You can also convert the double array into a logical array before 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 the index array, you can check the data type through the whos function, for example:

whos ind_double whos ind_logical
3) If you use floating point arithmetic to When computing indexed arrays, the array values may not be integer precision. If you know that the index value is very close to an integer, you can use the round function, for example:

A = [1 2 3 4; 5 6 7 8]; ind_float = 2.00001; ind_int = round(ind_float); A(ind_float)

4) When a variable with the same name as a MATLAB built-in function is defined, the function will be overwritten, resulting in the same Report an error (use the same brackets for the parameters and array index of the calling function), for example:

max = rand(5); A = rand(5); max(A)

At this time, you need to assign another variable name and clear the conflicting variable name:

B = max; clear max max(A)

The above is the detailed content of Array index must be a positive integer or logical value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn