Random split of a vector of unequal lengths

조회 수: 2 (최근 30일)
Adib Zaman
Adib Zaman 2014년 6월 25일
편집: Image Analyst 2014년 6월 25일
How can I split a vector in k unequal subsets? For example, if I have 200 data, a random split might give us 63, 95 and 150. That is 1:63, 64:95, 96:150 and 151:200.
Apology for cross postings.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 6월 25일
k=4
v=1:200
m=numel(v);
idx=unique([randperm(m-2,k)+1 m]);
idx0=[1 idx(1:end-1)+1];
out=arrayfun(@(ii,jj) v(ii:jj),idx0,idx,'un',0);
celldisp(out)

추가 답변 (2개)

Star Strider
Star Strider 2014년 6월 25일
One way to do it:
A = 1:200;
ndiv = 3; % NUMBER OF SUBMATRICES
idx = sort([1 randperm(length(A)-2, ndiv-1)+1 length(A)+1])
for k1 = 1:length(idx)-1
R{k1} = A(idx(k1):idx(k1+1)-1);
end
The logic guarantees that every element of R has at least two elements. Choose the number of sub-matrices with ndiv.

John D'Errico
John D'Errico 2014년 6월 25일
I don't see what is wrong with simply choosing k-1 values randomly between the min and max. Those points define a partition as you desire.
  댓글 수: 1
Adib Zaman
Adib Zaman 2014년 6월 25일
yes, the only problem is you need to sort it after the random number generation.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by