How I can randomly divide my data

조회 수: 3 (최근 30일)
Sehairi K.
Sehairi K. 2017년 9월 25일
편집: Jan 2017년 9월 26일
I have a matrix X with size nxm, How I can choose randomly 70 percent of this data and put it in matrix A and the rest in matrix B. I have already selected the 70% (according to rows) using the 'datasample' function, is there any tip to select the rest 30% of the data with there indices
[n,m]=size(X);
[A,idx] = datasample(X,round(0.7*n));
Thanks in advance for your help

채택된 답변

Jan
Jan 2017년 9월 25일
편집: Jan 2017년 9월 25일
[A, idxA] = datasample(X, round(0.7*n));
idxB = 1:n;
idxB(idxA) = [];
B = X(idxB);
This is slightly faster than:
idxB = setdiff(1:n, idxA);
  댓글 수: 3
Sehairi K.
Sehairi K. 2017년 9월 25일
I tride also that
row_idx = randperm(round(0.7*n))';
A_Idx = logical(zeros(size(X,1),1));
A_Idx(row_idx) = true;
B_Idx = ~A_Idx;
A_Data = Data(A_Idx,:);
B_Data = Data(B_Idx,:);
But I think your code is more optimized
Jan
Jan 2017년 9월 26일
편집: Jan 2017년 9월 26일
In the 2nd line the vector 1:n is created, and in the third line the indices used to create A are removed, such that you get the indices required for B.
The approach with randperm:
row_idx = randperm(n, round(n * 0.7)); % 2 inputs!
A_idx = false(n, 1); % logical(zeros()) wastes time
A = X(A_idx, :);
B = X(~A_idx, :);
I assume that this is faster than with datasample and the index vector.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resampling Techniques에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by