X= [P(1,:,:);
P(2,:,:);
P(3,:,:)];
y= P(4:end,:);
indTrain = randperm(4798);
indTrain = indTrain(1:3838);
trainX= X(indTrain,:);
trainy = y(indTrain);
indTest = 3839:4798;
indTest(indTrain) = [];
testX = X(indTest,:);
testy = y(indTest);
It shows error in trainX= X(indTrain,:);
saying
Index exceeds matrix dimensions
can anyone pls clarify ? thanks.
by the way I am having a 4x4798
data which my first 3 rows serve as predictors, and last row (4th row)
is my response. how would i correctly split the data into the first 3838 columns
as my training set and remaining as testing set.
Thanks..!!
To fix the indexing error you need to select the column indices of X
, rather than the row indices:
trainX = X(:, indTrain );
It seems like your P
matrix is 4-by-4798 and it is two dimensional. Therefore, writing P(1,:,:)
does select the first row, but it gives the impression as if P
is three dimensional because of the extra :
at the end. Don't do that . It's bad habit and makes your code harder to read/understand/debug.
X = P(1:3,:); % select all three rows at once
y = P(4,:); % no need for 4:end here - again, gives wrong impression as if you expect more than a single label per x.
Moreover, I do not understand what you are trying to accomplish with indTest(indTrain)=[]
? Are you trying to ascertain that the train and test set are mutually exclusive?
This line will most likely cause an error since the size of your test set is 960 and indTrain
contains 1:3838 (randomly permuted) so you will get "index exceeds..." error again.
You already defined your indTrain
and indTest
as mutually exclusive, no need for another operation. If you want to be extra careful you can use setdiff
indTest = setdiff( indTest, indTrain );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.