How to Fix the MATLAB Error: array indices must be positive integers or logical values

MATLAB is one of the most popular programming platforms for professionals and students who need to work with complex data. The platform highlights a combination of ease of use and surprisingly fast performance. You can use it to develop algorithms, create new models, and even integrate it with other platforms. MATLAB wasn’t just created as a way to work with data. The developers strive to make MATLAB a platform that fits the way experts think about complex data-related problems. However, that doesn’t mean there aren’t occasional stumbling blocks within the system.

Most data analysis platforms have custom types to hold and manipulate data. And learning how to use these types isn’t always intuitive. Different platforms handle collections in different ways. And you may face issues when making assumptions about how this works in one platform compared to another. This is the main reason why you might encounter an “array indices must be positive integers or logical values” error. You’ll soon see how to fix that problem and avoid it in the future. But in order to do so, we need to first take a deeper look at arrays in MATLAB.

Matrices and Arrays in MATLAB

MATLAB primarily stores data in matrices and arrays. If you’re familiar with arrays in NumPy, for example, you already have a good idea of how MATLAB handles the subject. However, there are some MATLAB array quirks to keep in mind. Indexing is one of the most important differences between MATLAB arrays and those found in many other platforms. When you’re working with a MATLAB array you need to make sure that indexes are provided as a positive integer or logical value. When you see the “array indices must be positive integers or logical values” error message it’s simply telling you that you’re using an improper value to access an array.

A Deeper Look at MATLAB Array Indexing

At first glance, you might think that using an improper value to access an array isn’t likely. After all, you’ve presumably thought out your program logic in advance. But this brings up something that’s both the best and worst thing about automated systems. Computer programs are great for automating tasks that would normally be time and labor-intensive. But those programs don’t really understand the context of a task. This means that they’ll happily substitute improper values into placeholders unless we’ve specifically put safeguards in place to make sure that doesn’t happen. This is why compilers often have warnings even when code cleanly compiles.

A compiler might note an instance where something could go wrong due to a variety of factors. This could include messy variable use or lack of safeguards. But MATLAB largely gives users a lot of freedom to push the system where they want it to go. And this can include instances of improper substitution. Take a look at the following code sample to see just how easy this mistake can be.

for x = 1:y:999
    if x == z

This is a simple for loop where we iterate through a range of numbers. This would be fine in theory. But what if y had a value of 0.6? This would instantly destroy x’s viability as an index value. This is due to the fact that MATLAB arrays use an indexing system that mirrors real-world storage. Imagine if you were sitting next to a collection of bulk food bins at the supermarket. An employee told you that the item you wanted was two bins up and one to the right of where your hand is currently resting. You wouldn’t have any success in opening a bin if you were to try to grab a non-existent handle that was two up and one half a bin to the right. Likewise, this concept holds true for the way array containers are implemented in MATLAB. It’s easy to round numbers to whole values in MATLAB. But this isn’t performed automatically when passing values.

How To Fix the Index Error

Thankfully the error is extremely easy to fix. The main point to keep in mind is that you always need to make sure you’re using a valid number as an index. For example, consider the following code.

ourTypes = {‘int8’};
ourRange = {‘>’,0,’<’,10};
X = round(A)

In this example we set a few attributes as the proper types we’re looking for. In this case, we go with a 1-byte (8-bit) signed integer that ranges up to 10. We then round the A variable to a whole number and pass it to the validateattributes function. This checks it against our criteria. We can use this process to validate variables before passing them on to use as an index value for our array.

Of course, the larger fix is to put some form of error checking into variable creation if it’s going to be leveraged within narrow usage scenarios. For example, you might set up a specific function to generate index values based on the current state of another variable. And validation based on criteria like we’ve used in the previous example could be easily baked into the specific generation function.

How to Fix the MATLAB Error: array indices must be positive integers or logical values
Scroll to top