Subscript indices must either be real positive integers or logicals, generic solution

前端 未结 3 1351
忘了有多久
忘了有多久 2020-11-21 06:08

The following error occurs quite frequently:

Subscript indices must either be real positive integers or logicals

I have found ma

3条回答
  •  逝去的感伤
    2020-11-21 06:39

    Subscript indices must either be real positive integers or logicals

    In nearly all cases this error is caused by one of two reasons. Fortunately there is an easy check for this.

    First of all make sure you are at the line where the error occurs, this can usually be achieved by using dbstop if error before you run your function or script. Now we can check for the first problem:

    1. Somewhere an invalid index is used to access a variable

    Find every variable, and see how they are being indexed. A variable being indexed is typically in one of these forms:

    variableName(index,index)
    variableName{index,index}
    variableName{indices}(indices)
    

    Now simply look at the stuff between the brackets, and select every index. Then hit f9 to evaluate the result and check whether it is a real positive integer or logical. Visual inspection is usually sufficient (remember that acceptable values are in true,false or 1,2,3,... BUT NOT 0) , but for a large matrix you can use things like isequal(index, round(index)), or more exactly isequal(x, max(1,round(abs(x)))) to check for real positive integers. To check the class you can use class(index) which should return 'logical' if the values are all 'true' or 'false'.

    Make sure to check evaluate every index, even those that look unusual as per the example below. If all indices check out, you are probably facing the second problem:

    2. A function name has been overshadowed by a user defined variable

    MATLAB functions often have very intuitive names. This is convenient, but sometimes results in accidentally overloading (builtin) functions, i.e. creating a variable with the same name as a function for example you could go max = 9 and for the rest of you script/function Matlab will consider max to be a variable instead of the function max so you will get this error if you try something like max([1 8 0 3 7]) because instead of return the maximum value of that vector, Matlab now assumes you are trying to index the variable max and 0 is an invalid index.

    In order to check which variables you have you can look at the workspace. However if you are looking for a systematic approach here is one:

    For every letter or word that is followed by brackets () and has not been confirmed to have proper indices in step 1. Check whether it is actually a variable. This can easily be done by using which.


    Examples

    Simple occurrence of invalid index

    a = 1;
    b = 2;
    c = 3;
    a(b/c)
    

    Here we will evaluate b/c and find that it is not a nicely rounded number.

    Complicated occurrence of invalid index

    a = 1;
    b = 2;
    c = 3;
    d = 1:10;
    a(b+mean(d(cell2mat({b}):c)))
    

    I recommend working inside out. So first evaluate the most inner variable being indexed: d. It turns out that cell2mat({b}):c, nicely evaluates to integers. Then evaluate b+mean(d(cell2mat({b}):c)) and find that we don't have an integer or logical as index to a.

    Here we will evaluate b/c and find that it is not a nicely rounded number.

    Overloaded a function

    which mean 
    % some directory\filename.m
    

    You should see something like this to actually confirm that something is a function.

    a = 1:4;
    b=0:0.1:1;
    mean(a) = 2.5;
    mean(b);
    

    Here we see that mean has accidentally been assigned to. Now we get:

    which mean
    % mean is a variable.
    

提交回复
热议问题