• Register
0 votes
143 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 84 views
84 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
1 vote
1 answer 11 views
11 views
Problem: I keep getting tihs error how can I solve this : Subscript indices must either be real positive integers or logicals
asked Apr 27 Raj Tyagi 15.6k points
0 votes
1 answer 5 views
5 views
Problem: Hi ! I am facing this issue can any body help please?
asked Mar 18 PkGuy 23.5k points
0 votes
1 answer 10 views
10 views
Problem: Why I got the error >Why I got the error > Subscript indices must be either positive integers less than 2^31 or logicals
asked Apr 29 ummeshani 10.8k points
0 votes
1 answer 6 views
6 views
Problem: 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= ... 60x10, and "Symbols" is 10x1 string. One more question :) what's exactly meant by "subscript indices"? MUCH appreciated in advance xx
asked Apr 5 nabin shrestha 4.3k points
0 votes
1 answer 5 views
5 views
Problem: How do I do it ? >Matlab array indices must be positive integers or logical values
asked May 4 ochena 23.3k points
0 votes
1 answer 4 views
4 views
Problem: When I type, [f]= NC(-3,0,fun) and set fun= @(x)normpdf(x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
asked May 3 sumaiya simi 43.9k points
0 votes
1 answer 8 views
8 views
Problem: Problem Details and clues %For this problem write a script file called NC.m that implements %the Newton-Cotes method of integration for an arbitrary function f(x). It %should take as inputs the function and the limits of integration [a: b] and %output the ... (x) . it keeps on returning "Array indices must be positive integers or logical values". Can someone shine some light on this?
asked May 2 ochena 23.3k points
0 votes
1 answer 14 views
14 views
Problem: This code seems like it should be simple, but for some reason I keep getting this error: "Index in position 2 is invalid. Array indices must be positive integers or logical values." It refers to these lines: Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2))); VR(1,i) = Vr; I've tried checking if I ... = 0:1000 w=i*10*10^-9; Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2))); VR(1,i) = Vr; end Thanks!
asked Apr 25 muktaa 34.6k points