MATLAB Answers


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

Why do I get the following error message:

 Subscript indices must either be real positive integers or logicals. 


No tags entered yet.


10 Answers

Answer by MathWorks Support Team on 12 Jun 2017
 Accepted Answer

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);

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);

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); 

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:


Show 1 older comment

duguyihan comments "I have the same problem with two different 'min'."

Thank you, you just saved my life.... I almost died looking for the issue, and it turns out I had also named a variable "min"

Sign in to comment.

Answer by Antoine Pichot on 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 Comment

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.

Sign in to comment.

Answer by Schrecklich Er on 18 Mar 2017
Edited by Walter Roberson
on 4 Apr 2017

I got this message while I was trying process a image using 'imread', i had the following structure:

for i = 1 : f
      for j = 1 : c

i got the same error message so until 30 minutes of research I just switch the letter 'i' for a 'k' and the error message disapeared, I think that the error was there because the letter 'i' is used for imaginary numbers.

just a little hint! Hope it is useful.


Sign in to comment.

Answer by Farah Nadiah on 22 Apr 2016

how about this. the word that have can i declare..if i run this prgoram it get error..thnx a lot

         for i=0:1: maxrow-1 
             for j=0:1: maxcol-1
             % inv([i, j]) = 255 - image([i, j]);
                 for k = 0: 1
                 *sto([i, j, k]) = image([i, j]);*

  1 Comment

MATLAB indexing starts at 1, not at 0. You need to add 1 to all of your indices.

Also remember that sto([i, j, k]) is indexing sto at 3 locations, sto(i), sto(j), sto(k). It is not an index into a 3 dimensional array: that would be sto(i, j, k)

Sign in to comment.

Answer by Pratyush Lohumi on 18 Mar 2017
Edited by Walter Roberson
on 18 Mar 2017

for n = 0:ns
s(n) = (ns-n)/ns; %slip
Tmech(n) = ph*V1eq^2*R2/((s(n)*omegas)*((R1eq + R2/s(n))^2+(X1+X2)^2)); %Electromechanical torque
end %End of slip loop

Error: Subscript indices must either be real positive integers or logicals. (Line 2)

Query: Couldn't seem to rectify the mistake?

If someone could provide a valid explanation or a corrected code for this loop, that would really help my project.


In MATLAB, subscripts start at 1. Your n starts at 0, so s(n) is going to start at s(0) which is not permitted.

nvals = 0 : ns;
for nidx = 1 : length(nvals)
  n = nvals(nidx);
  s(nidx) = (ns-n)/ns; %slip
  Tmech(nidx) = ph*V1eq^2*R2/((s(nidx)*omegas)*((R1eq + R2/s(nidx))^2+(X1+X2)^2)); %Electromechanical torque

This can be vectorized, no loop:

s = linspace(1, 0, ns+1);
Tmech = ph .* V1eq.^2 .* R2 ./ ((s .* omegas) .* ((R1eq + R2./s).^2 + (X1+X2).^2));

If you insist on using the same structure of loop, then:

for n = 0:ns
s(n+1) = (ns-n)/ns; %slip
Tmech(n+1) = ph*V1eq^2*R2/((s(n+1)*omegas)*((R1eq + R2/s(n+1))^2+(X1+X2)^2)); %Electromechanical torque
end %End of slip loop
 for m = 1:num_pulse_int
    %Update sensor and target positions
    [sensorpos,sensorvel] = sensormotion(1/prf);
    [tgtpos,tgtvel] = tgtmotion(1/prf);

Sign in to comment.

Answer by padmini kutturu on 26 Apr 2017
Edited by Walter Roberson
on 26 Apr 2017

Can someone please help me with this? Thank you!

for j=1:n
    y(j)= (T(j)-Ts)/(Tb-Ts);

  1 Comment

On the first iteration, j = 1. Then you have


which uses y(j-1) which would be y(0) .

If you are going to use y(j-1) then your j must start at 2 or more.

Sign in to comment.

Somenone can help me. I got this erro when using this code below:

W2=[];%will contain watermark signal extracted from the image
for t=1:wmsz
W2 = [W2(D_w(IND(t,1),IND(t,2))/D(IND(t,1),IND(t,2)))*10] %watermark extraction

  1 Comment

Let us match brackets. The number will be the count of open brackets "after" the character aligned with:

W2 = [W2(D_w(IND(t,1),IND(t,2))/D(IND(t,1),IND(t,2)))*10]
     1  2   3   4   3    4   32  3   4   3    4   321   0

We see from this that W2 is being indexed by

D_w(IND(t,1),IND(t,2)) / D(IND(t,1),IND(t,2))
   1   2   1    2   10    1   2   1    2   10

which contains a division. So for the index into W2 to be an integer, D_w(IND(t,1),IND(t,2)) would have to be an non-zero integer exact integer multiple of D(IND(t,1),IND(t,2)) . That condition is not impossible, but it is something I would tend to doubt.

Sign in to comment.

Answer by Yago Veloso on 6 Oct 2017
Edited by Walter Roberson
on 6 Oct 2017

Hi everyone! I get the same error in my code, I'm trying to connect a function to my main code, where the function supply my code with all variables of my system equation solution.

Below is part of my main code where I found this error:

[Y1des]= ANN (Z1,e1,s1,D);
[ ac, ao, at_coluna, kp, r1, r2, L, vj, ro_ar, visc_ar, k_ar, RA, P, t_ar, Tar_e, T_ar_ext, Urel_e, Urel, Uabs_e, cps, ro_p_ap, hparede, cal_lat, cpar, cpl, cpv, ro_p, dp, Ubs, u, Rep1, Nu, hp1, St, Rep2, hw1, e, ae1, aet, X1, t_seg, X, ae, hp, hw, d_xp, tt,z]= variaveis (h, Y1des, hi, uar, t_min);
[G ,u, Pvse, Pve, UAEsat, UAE, He]= prop_ar (ro_ar,uar,ac, ao, at_coluna,t_ar, Tar_e, Urel_e, P);
[vl, mg, mss]= prop_leito (ro_p_ap,ro_p,ro_ar,ac,hi,vj);
%%% Initial condition 
Tp(1)= 298.15;
Tar_s(1)= Tar_e - ((1-exp(-X(1)))*(Tar_e-Tp(1))); 
tar_s(1) = Tar_s(1)-273.15;

Here is the error message

Subscript indices must either be real positive
integers or logicals.
Error in Dif_finitas_plus_ANN (line 44)
Tar_s(1)= Tar_e -

Thanks for the help !!


Start a new question for this. Don't tack it on here.

Walter Roberson, thanks for the help! That's what was causing the error.

Sign in to comment.

Answer by MarkusP
on 5 Feb 2018
Edited by MarkusP
on 5 Feb 2018

hello guys, maybe someone of you is able to help me...I tried to fix my problem with the solution above but it wasn`t possible.

I get the same error message if I`m runnung the following skript. The skript is for solving a problem with methods of characteristic.

for i=2:n
for it=1:itmax
    for i=2:n-1
      pc(n)=pa-rho*a(vc(n)-va)+f*h*rho/(2*d)*va*abs(va);      %% error is in this line

My valve(t) function looks like this:

function vrel = valve(t)
if t<.1

Here is the error message:

Subscript indices must either be real positive integers or logicals.
Error in MOCwaterhammer (line 45)

Thanks for you help!


Sign in to comment.

Answer by Torsten
on 5 Feb 2018
Edited by Torsten
on 5 Feb 2018

You forgot a multiplication sign:


Best wishes


  1 Comment

Oh the solution can be so simple...

Thank you so much!

Sign in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today