• Register
0 votes
104 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.

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

It is very important to know the powerful yet very intuitive feature of the matrix indexing in Matlab. So how indexing actually works and what is the valid index would help avoid above error in a first place by using the valid indices.

At its very core, given the array A of length n, there are two ways to index it.

1. Linear indexing: With a subset of integers from 1 : n ,0 is not allowed, as the Matlab arrays are 1-based, unless you use a method below. For a higher-dimensional arrays, multiple subscripts are the internally converted into a linear index, although in the efficient and transparent manner.

2. Logical indexing: When you use the n-length array of 0s and 1s, to just pick those elements where the indexing is true. In this case, the unique(index) must have only 0 and 1.

So the valid indexing array into another array with the n number of elements can be:

1. Entirely logical of a same size, or

2. Linear with a subsets of integers from 1:n

So invalid indexing error always occurs when you try to mix the two types of indexing: one or more zeros occur in the linearly indexing array, or you just mix 0s and 1s with anything other than 0s and 1s.

9 7 4
38,600 points

Related questions

0 votes
2 answers 69 views
69 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); ... is the price matrix of 60x10, and "MySymbols" is the 10x1 string. One more doubt from my side what are "Subscript indices"?
asked Dec 25, 2019 alecxe 7.5k points
0 votes
2 answers 542 views
542 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 4 views
4 views
typeerror: tuple indices must be integers or slices, not tuple
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 5 views
5 views
Problem: So, I'm not entirely sure what's going on here, but for whatever reason Python is throwing this at me. For reference, it's part of a small neural network I'm building for fun, but it uses a lot of np.array and such, so there's a lot of matrices ... type clash. Maybe somebody can help me figure this out, because I've been staring at this error for too long without being able to fix it.
asked Feb 16 Mashhoodch 9.1k points
0 votes
1 answer 46 views
46 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.8k points
0 votes
1 answer 29 views
1 vote
1 answer 81 views
81 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 79 views
79 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 275 views
275 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