Splitting a matrix into two smaller matrices in a loop

3 views (last 30 days)
Suppose i have a small matrix of size 10x10. I also have an array which has predifiened indexes it looks like this as an example: ind = [1;2;4;6;8;10].
I simply want to run a loop in which the original matrix will be split into train and test matrices. In first iteration the test matrix will have number of rows (or sample) according to ind(1) and all columns (or features) and rest of all the rows will go in train matrix. Similarly, in second iteration the test matrix will have number of rows (or sample) according to ind(2) and all columns (or features) and rest of all the rows will go in train matrix. The logic is that those rows which appear in test will not appear in train in that iteration. How can i achieve this? I was able to define Test matrix but 't unable to come up with the logic for train matrix. please look at the code i provided.
with these train and test set i want to train and test SVM and calculate the accuracy.
As you gussed it im trying to do Leave one out cross validation in my own way!
% My_Data is the 10x10 matrix. it has random values
z=[1;2;4;6;8;10]; % specific indexs
for k=1:length(z)
test=My_Data(z(k):z(k+1),:);
% how to define train set
end

Accepted Answer

Matt J
Matt J on 22 Apr 2021
z=[1;2;4;6;8;10];
for k=1:length(z)
ind=z(k):z(k+1);
test=My_Data(ind,:);
train=MyData;
train(ind,:)=[];
end

More Answers (0)

Categories

Find more on Parallel and Cloud in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!