필터 지우기
필터 지우기

Splitting a table to 2

조회 수: 8 (최근 30일)
Rafi Kandli
Rafi Kandli 2017년 8월 16일
댓글: KL 2017년 8월 16일
Hello all, I have a table named A. I want to split it into 2 sub-tables: Table 1 has 60% of the rows in A, chosen randomly. Table 2 has all the other rows that are not in table 1. Any elegant idea on how to do this?

채택된 답변

KL
KL 2017년 8월 16일
dummy = 10*rand(10,4);
T = array2table(dummy)
ranRows = randperm(length(dummy));
T1 = T(ranRows(1:(0.6*length(dummy))),:)
T2 = T(ranRows((0.6*length(dummy))+1:end),:)
  댓글 수: 2
Guillaume
Guillaume 2017년 8월 16일
The proper function for getting the height of a table is height, so:
ranrows = randperm(height(T));
T1 = T(ranRows(1:0.6*height(T)), :);
T2 = T(ranRows(0.6*height(T)+1:end), :);
To avoid a warning, I would also wrap the 0.6*height(T) calculation inside a ceil, a floor or a round.
KL
KL 2017년 8월 16일
Thanks Guillaume!

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

추가 답변 (2개)

Adrian
Adrian 2017년 8월 16일
편집: Adrian 2017년 8월 16일
Note that this will only play nicely if you have a reasonably large array.
selection = randi([1 10], size(A,1),1)
A_sixty = A(selection>4);
A_forty = A(selection<=4);

Sean de Wolski
Sean de Wolski 2017년 8월 16일
If you're looking to split your data into a training and testing set, then it might pay to look at cvpartition as well, it provides more options for splitting tables.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by