# Why do I get the error 'Subscript indices must either be real positive integers or logicals. ' ?

306 visualizzazioni (ultimi 30 giorni)
MathWorks Support Team il 7 Mag 2013
Why do I get the following error message:
ERROR: Subscript indices must either be real positive integers or logicals.

### Risposta accettata

MathWorks Support Team il 17 Feb 2021
Modificato: MathWorks Support Team il 17 Feb 2021
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.
2) 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:
whos ind
3) 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.
ind == round(ind)
4) 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:
##### 2 CommentiMostra NessunoNascondi Nessuno
Walter Roberson il 5 Apr 2016
duguyihan comments "I have the same problem with two different 'min'."
Walter Roberson il 25 Set 2018
"Has this been changed in a recent update of Matlab? (I updated from 2013 to 2017). Before that I never had to convert from double to logical."
R2013a:
>> A=rand(1,3), A([1 0 0])
A =
0.8147 0.9058 0.1270
Subscript indices must either be real positive integers or logicals.
Same with R2010bSP1, which is the oldest MATLAB I happen to have installed at the moment.
If we examine the R14 documentation, https://www.mathworks.com/help/releases/R14/techdoc/matlab.html in the document for " Special Characters [ ] ( ) {} = ' . ... , ; : % ! @ " we see,
"The components of V must be integers to be used as subscripts. An error occurs if any such subscript is less than 1 or greater than the size of X."
This is not a new restriction, and No, there is no setting in MATLAB that could change this.

Accedi per commentare.

### Più risposte (2)

Antoine Pichot il 19 Nov 2015
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.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Stephen23 il 19 Nov 2015
Modificato: Stephen23 il 19 Nov 2015
It is already part of the answer: "Another common cause is that a variable has overwritten a function name and is thus shadowing the function. For example:..." and it then proceeds to give an example of how this can happen.

Accedi per commentare.

Torsten il 5 Feb 2018
Modificato: Torsten il 5 Feb 2018
You forgot a multiplication sign:
pc(n)=pa-rho*a->*<-here(vc(n)-va)+f*h*rho/(2*d)*va*abs(va);
Best wishes
Torsten.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

### Tag

Non è stata ancora inserito alcun tag.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by