필터 지우기
필터 지우기

Split a table in two different tables

조회 수: 2 (최근 30일)
Rachele Franceschini
Rachele Franceschini 2021년 9월 1일
댓글: Walter Roberson 2021년 9월 1일
Hi, Can You help me?
I have a table C. I would like to split in 2 tables. In table 1 there is 30% of the data (chosen randomly) and in the second table there is the 70% of the data.
Do you have a tips?

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 1일
In the case that you want the "30%" to be as exact as round-off will permit:
r = height(C);
ridx = randperm(r);
n1 = floor(r * .3);
n2 = r - n1;
C1 = C(sort(ridx(1:n2)),:);
C2 = C(sort(ridx(n2+1:end),:));
In the case that 30% should be statistical:
r = height(C);
mask = rand(r,1) <= 0.3;
C1 = C(mask,:);
C2 = C(~mask,:);
  댓글 수: 2
Rachele Franceschini
Rachele Franceschini 2021년 9월 1일
Thank you so much!
I tried both your code. The first, I have error, matlab didn't recognize the function "sort".
But the second code, it is perfect. Thank you so much!!!
Walter Roberson
Walter Roberson 2021년 9월 1일
In the first one,
C2 = C(sort(ridx(n2+1:end)),:);
) was in the wrong place.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by