randomly divide a matrix

hi, i have a 16435*25 matrix that name is input, i want to randomly divided it in 2 parts: one for train and another for validate data, that 70% of its rows randomly selected as train matrix (i.e 11504*25 matrix) and 30% of its rows randomly selected as validate matrix (i.e 4930*25), how can i do this? thanks a lot

 Risposta accettata

Star Strider
Star Strider il 21 Mag 2017
For your matrix:
row_idx = randperm(16435, 11504)';
To illustrate:
example = randperm(10, 7)'
example =
5
8
2
4
7
3
10

4 Commenti

baran
baran il 21 Mag 2017
thanks but i dont want to select random element, i want to select random rows from input matrix . the size of input matrix is 16403 rows and 25 column, and i want to divide this matrix in two part: 1. a matrix with 11504 rows and 25 column that is randomly selected from input rows. 2. a matrix with 4930 rows and 25 column that is randomly selected from input rows. how can i do this?
My code will create the random row indices for you. To create your training and testing matrices, use this code with your own ‘Data’ matrix. I chose a shorter matrix for convenience.
The Code
Data = randi(9, 10, 5) % Create Data Matrix
row_idx = randperm(10, 7)' % Vector Or Random Rows — Use The ‘row_idx’ I Created For Your Larger Matrix In Your Code
Train_Idx = logical(zeros(size(Data,1),1)); % Logical Vector
Train_Idx(row_idx) = true; % Training Rows
Test_Idx = ~Train_Idx; % Testing Rows
Train_Data = Data(Train_Idx,:)
Test_Data = Data(Test_Idx,:)
Data =
7 9 4 1 9
8 1 3 8 9
7 8 9 9 9
3 5 5 2 9
1 9 7 5 7
2 8 6 7 7
3 2 8 1 4
8 4 3 1 1
9 1 7 4 4
5 4 4 2 5
row_idx =
1
8
4
5
10
2
9
Train_Data =
7 9 4 1 9
8 1 3 8 9
3 5 5 2 9
1 9 7 5 7
8 4 3 1 1
9 1 7 4 4
5 4 4 2 5
Test_Data =
7 8 9 9 9
2 8 6 7 7
3 2 8 1 4
This shows both how it works and that it works!
Onurcan BAL
Onurcan BAL il 6 Giu 2021
Thanks for both question and this explanation!
Star Strider
Star Strider il 6 Giu 2021
My pleasure!
(A Vote would be appreciated!)
.

Accedi per commentare.

Più risposte (1)

MathReallyWorks
MathReallyWorks il 21 Mag 2017
Modificato: MathReallyWorks il 21 Mag 2017
Hello Dear,
Use this code. I've generated a random matrix of the same order that you want. I have randomized all the rows of matrix and then I'm selecting first 11504 rows for training and rest for validation. I hope it will be helpful.
orderedArray = rand(16435,25); % Random Data %You can use your data here
shuffledArray = orderedArray(randperm(size(orderedArray,1)),:); %Randomizing the rows of matrix
t=zeros(11504,25); % Size of Train Data
v=zeros(4930,25); % Size of Validate Data
for i=1:11504
t(i,:) = shuffledArray(i,:);
end
j=1;
for i=11541:16435
v(j,:) = shuffledArray(i,:);
j=j+1;
end
Type whos t and whos v on command window, you will get to know the dimensions of train and validate data matrices.

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by