Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

matlab

I'm currently running my code, and found an annoying problem that I really haven't got any idea to solve.

The function that I'm working out is as follows;

function out = CointPairs(PriceMat, Symbols)
    out=[];
    NofStocks = size(PriceMat, 2);
    CointMatrix= zeros(NofStocks);

    [rows, cols] = find(CointMatrix);
    CointPairs = [rows, cols];
    **cf= (CointPairs(:,1)-CointPairs(:,2))==0;**
    CointPairs(cf,:) = [];

    if(isempty(CointPairs))
        warning('No Cointegrated Pairs Found')
        return
    end
end

And the bloody problem occurs at this line:

cf= (CointPairs(:,1)-CointPairs(:,2))==0;

saying that "Subscript indices must either be real positive integers or logicals." well, the input variable "PriceMat" is the price matrix 60x10, and "Symbols" is 10x1 string.

One more question :) what's exactly meant by "subscript indices"?

MUCH appreciated in advance xx

like image 948
Vivian Avatar asked Mar 18 '23 20:03

Vivian


2 Answers

Let's 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 60x10, and "Symbols" is 10x1 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 it's 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 you're getting that error is because you are trying to do operations on an empty matrix when that's not allowed. You need to move your if statement before you do CointPairs = [rows, cols];. That way, you won't 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 :) what's 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!

like image 104
rayryeng Avatar answered Apr 26 '23 23:04

rayryeng


"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..

like image 33
lakshmen Avatar answered Apr 27 '23 01:04

lakshmen