Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

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

Date: 2022-06-28 11:42:54

The following error occurs quite frequently: 

Subscript indices must either be real positive integers or logicals 

I have found many questions about this but not one with a really generic answer. Hence I would like to have the general solution for dealing with this problem. 

Expert Answer:

s: 

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.

 

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com