0

I'm trying to split data into a training and testing set. The data X is a 150x4 matrix of 150 data points with 4 features each. I did this to create an index vector to randomly select 100 out of 150 data points for training:

trainIndices = zeros(length(X),1);
trainIndices(randperm(150,100)) = 1

Then I tried doing this to select the rows where trainIndices == 1:

X_train = X(trainIndices,:);

But I'm getting an error Subscript indices must either be real positive integers or logicals.

What am I doing wrong here?

Austin
  • 6,921
  • 12
  • 73
  • 138

2 Answers2

3

Since trainIndices is of type double, MATLAB is trying to treat the values as indices. Instead, you'll want to explicitly cast trainIndices as a logical matrix so that it can be used to perform logical indexing

trainIndices = false(length(X),1);
trainIndices(randperm(150,100)) = true;

X_train = X(trainIndices,:);

Or you can use your existing trainIndices and cast it

X_train = X(logical(trainIndices),:);

I would recommend the first approach since the logical array will take up less memory than the double array.

Graham
  • 7,431
  • 18
  • 59
  • 84
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thanks that worked will accept this answer as soon as it allows – Austin Feb 16 '17 at 18:43
  • I'd link to http://stackoverflow.com/documentation/matlab/750/vectorization/9514/logical-masking#t=201702161856275395681 to help clarify this technique. – Dev-iL Feb 16 '17 at 18:57
2

Indexing in MATLAB can be either linear or logical or a combination of them. Linear indexing is the regular indexing as C/C++ using integer numbers [1,n] (with n being vector length). Here you've tried to use a double vector (trainIndices) for logical indexing and since there is no element with index 0 MATLAB throws an error. The problem is solved through the following logical conversion:

X_train = X(trainIndices>0,:);

For more info on indexing you might see:

http://matlabtricks.com/post-23/tutorial-on-matrix-indexing-in-matlab

and

Linear indexing, logical indexing, and all that

Community
  • 1
  • 1
Mehdi
  • 293
  • 4
  • 13
  • @LuisMendo, I think you're right , they are not completely separate things. I'll modify the answer and steal some parts of your comment! – Mehdi Feb 17 '17 at 05:06