# Subscript indices must either be real positive integers or logicals. matlab

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.

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

## Related questions

27 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"?
1 vote
11 views
Problem: I keep getting tihs error how can I solve this : Subscript indices must either be real positive integers or logicals
5 views
Problem: Hi ! I am facing this issue can any body help please?
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
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
5 views
Problem: How do I do it ? >Matlab array indices must be positive integers or logical values