Random split of a vector of unequal lengths
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Adib Zaman
am 25 Jun. 2014
Bearbeitet: Image Analyst
am 25 Jun. 2014
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.
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 25 Jun. 2014
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)
Weitere Antworten (2)
Star Strider
am 25 Jun. 2014
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.
0 Kommentare
John D'Errico
am 25 Jun. 2014
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.
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!