MATLAB error: Subscript indices must either be real positive integers or logicals

MATLAB error: Subscript indices must either be real positive integers or logicals

Lets tackle your questions one at a time:

Subscript indices must either be real positive integers or logicals. well, the input variable PriceMat is the price matrix 60×10, and Symbols is 10×1 string.

Take a look at your CointPairs variable. This is the result from using the find command. There may be a case where CointPairs produces the empty matrix. This is because when you run find, there may be a case where there is no entry in your CointMatrix where its equal to 1 (or non-zero actually). If this is the case, then CointPairs will actually be empty as there are no elements that meet the requirements.

As such, the reason why youre getting that error is because you are trying to do operations on an empty matrix when thats not allowed. You need to move your if statement before you do CointPairs = [rows, cols];. That way, you wont get any accessing errors. In other words, do this:

[rows, cols] = find(CointMatrix);
CointPairs = [rows, cols];

%// Move here
if(isempty(CointPairs))
    warning(No Cointegrated Pairs Found);
    out = []; %// Make output empty - Must return something or you get an error
    return
end

%// Continue here
cf= (CointPairs(:,1)-CointPairs(:,2))==0;
CointPairs(cf,:) = [];

%// More code here...
%// ...

One minor comment I have is that your output variable is out, but you are not assigning it anywhere in your code. Is this intentional?


One more question 🙂 whats exactly meant by subscript indices?

Subscript indices are those values you use to access elements in your array / matrix. For example, suppose your matrix is:

A = [1 2 3; 4 5 6; 7 8 9];

By doing A(2,2), we get the element 5. The row 2 and the column 2 is known as subscript index. Indices implies more than one, so instead of just a single pair of row and column locations, you can use two arrays of elements to access the rows and columns of your matrix. Each pair of corresponding elements in the pair is a subscript index.

Basically, they are numbers that you use to access the rows and columns of your matrix. You can only access elements in matrices / arrays using positive numbers (a.k.a. 1, 2, 3, 4…) or logical operators (i.e. true / false). The empty matrix, 0, negative integers or floating point numbers are not allowed.

Because you are accessing your matrix using neither of the above valid inputs, you get that error.


Hope this helps!

Subscript indices must either be real positive integers or logicals. means the index that you are trying to reference does not exist. Most probably it is CointPairs(:,2) does not exist.

So my suggestion is to put a break point in the line CointPairs = [rows, cols]; and see the size of the CointPairs matrix. and see whether the second column exist..

Hope this helps..

MATLAB error: Subscript indices must either be real positive integers or logicals

Here is another possibility that took me a bit to figure out…

I defined

% as a variable
plot = 1;

Basically I overloaded MATLABs plot function which is a big no-no!!

Leave a Reply

Your email address will not be published.