• Register
0 votes
67 views

Problem  :

I am currently trying to run my code but found the really annoying problem which I am unable to solve.

The function that I am currently working out is as below;

function myout = MyCointPairs(MyPriceMat, MySymbols)

   myout=[];

   MyNofStocks = size(MyPriceMat, 2);

    MyCointMatrix= zeros(MyNofStocks);

    [rows, cols] = find(MyCointMatrix);

    MyCointPairs = [rows, cols];

    **mycf= (MyCointPairs(:,1)-MyCointPairs(:,2))==0;**

    MyCointPairs(mycf,:) = [];

    if(isempty(MyCointPairs))

        warning('No Cointegrated Pairs Found Here')

        return

    end

end

And the problem generally occurs at below line:

mycf= (MyCointPairs(:,1)-MyCointPairs(:,2))==0;

Saying that the "Subscript indices must either be real positive integers or logicals." And the input variable "MyPriceMat" is the price matrix of 60x10, and "MySymbols" is the 10x1 string.

One more doubt from my side what are "Subscript indices"?

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

"Subscript indices must either be real positive integers or logicals." This means the index that you want to reference does not really exist. Most probably it means MyCointPairs(:,2) does not really exist.

So my suggestion to you is to put the break point in the following line MyCointPairs = [rows, cols]; and observe the size of your MyCointPairs matrix. and also try to see whether your second column really exist or not.

Hope this helps you in clearing all your doubt and resolves your errors.

9 7 4
38,600 points
0 votes

This error occurs when the user attempts to index an array using the indices that are not positive integers or logical values.

Solutions:

Here are some common solutions for this error;

Double-check that your indices are positive integers, Indices in MATLAB cannot be p and by default start with 1.

Logical indexing:

If you are using the logical indexing to index the array, then make sure that your index array is of type “logical” and not a “double” array of 1s and 0s. You can convert the “double” array to a logical array before attempting to use logical indexing.

Example:

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

ind_double = [0 1 0 1; 0 1 0 1];

ind_logical = logical(ind_double);

A(ind_logical)

For index array ‘ind’ you can check its data type by using the whos function;

whos ind

Floating-point arithmetic:

If you are using the floating-point arithmetic to compute the index array, then the array values may not be an integer. The “round” function is useful when you know you have an index value that is nearly integer index according to your needs.

Example:

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

ind_float = 2.00001;

ind_int = round(ind_float);

A(ind_float)

Below is the method to check if an index array “ind” contains exact integer values. This command returns a logical array where 1 indicates the index value is an exact integer and o indicated it is not.

ind == round(ind)

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 92 views
92 views
Problem : The below error occurs quite frequently to me: “Subscript indices must either be real positive integers or logical” I want to have a general solution for dealing with above problem.
asked Jan 28, 2020 jwilliam 3.9k points
0 votes
2 answers 433 views
433 views
Problem : I am facing following error message: Index in position 1 is invalid. Array indices must be positive integers or logical values. My code is as follows: clc; close all; clear; PD=phantom('Modified Shepp-Logan'); padimage = [2,2]; PD= padarray(PD, ... : Index in position 1 is invalid. Array indices must be positive integers or logical values. Can anybody tell me why the issue is here?
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 9 views
9 views
Problem: Hello! I am completely new to Python and trying to grasp concepts in Python. While I was learning lists and dictionaries, I wrote the following program: name = ["John", "Alice", "Catherine"] diction = {"name" : "Alice"} print(name[diction]) When I ... ; as a value and the program should print “Alice” on screen. I do not know what I have done wrong. Can anybody help?
asked Dec 18, 2020 Code Learner 5.1k points
0 votes
1 answer 20 views
1 vote
1 answer 66 views
66 views
Problem : I am very new to Programming. Currently I am trying to learn the Python. I am trying to slice my list (myplateau) in the several list (L[i]) but because of some unknown reasons to me I am facing the below error message: File "C:\Users\Martin\Skycraper ... ]     L.append(myitem)     i += 1 I am very confused about above error. Please help me in fixing above mentioned python related error.
asked May 13, 2020 Martin K 6.6k points
1 vote
2 answers 44 views
44 views
Problem: I wrote a program: def only(): names = ['Laia', 'Poter', 'Harrold', 'Annabell', 'Preety', 'Whina', 'Cory', 'Phase'] put = input('Search with name:') binary_search(names, put) if position == -1: print("The name entered is not part ... in binary_search if names[middle] == entered: TypeError: list indices must be integers or slices, not float Why is this error happenning can anyone explain?
asked Apr 3, 2020 LizzyM 6.1k points
3 votes
2 answers 227 views
227 views
Problem: I am trying to learn python3 and tuple. It was going pretty smooth unless I tried to access an element from a tuple of my program. It throws back an error: typeerror: tuple indices must be integers or slices, not tuple What's wrong? Any explanation?
asked Mar 25, 2020 Gavin 15.3k points
1 vote
3 answers 616 views
616 views
Problem : I'm a newbie with Python's arrays and I don't understand how I can avoid below error: python typeerror: list indices must be integers or slices, not str
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 114 views
114 views
Problem: I am trying to plot two vectors in MATLAB and it keeps giving me the error that error in plot vector must be same length. What to do? Some one please help.
asked Oct 10, 2020 Ahsan 1.8k points