randomly divide vector into 2 parts.

3 ビュー (過去 30 日間)
RUCHI CHOUDHARY
RUCHI CHOUDHARY 2019 年 9 月 15 日
編集済み: Bruno Luong 2019 年 9 月 15 日
i have vector 100000*1 .i want to randomly divide data of it into 80% and 20% and store them into 2 vector.Divide of data must be random.
"non_zero_entry" is total how many entry in the vector.i tried to do this but it show me error "Index exceeds the number of array elements (80000)".
training_data=.8;
tf = false(length(non_zero_entry),1);
tf(1:round(training_data*non_zero_entry)) = true;
tf = tf(randperm(non_zero_entry)); % error occur in this line "Index exceeds the number of array elements (80000)".
dataTraining = index_rating(tf,:);
dataTesting = index_rating(~tf,:);
Thanks in advance for your help.

採用された回答

Rik
Rik 2019 年 9 月 15 日
I don't fully understand every step of your code, so I rewrote it:
index_rating=rand(100000,5);%generate example data
training_data=.8;
s=rand(size(index_rating,1),1);
sorted=sort(s);
cutoff=sorted(round(training_data*numel(s)));
tf=s<=cutoff;
dataTraining = index_rating(tf,:);
dataTesting = index_rating(~tf,:);

その他の回答 (2 件)

Bruno Luong
Bruno Luong 2019 年 9 月 15 日
編集済み: Bruno Luong 2019 年 9 月 15 日
For the record, here is how I would do:
n = size(index_rating,1);
i = randperm(n,round(training_data*n));
dataTraining = index_rating(i,:);
dataTesting = index_rating(setdiff(1:n,i),:);

Bruno Luong
Bruno Luong 2019 年 9 月 15 日
編集済み: Bruno Luong 2019 年 9 月 15 日
Always post complete code that can run. don't less us geuss what is the size/class of the inputs
First assumption: non_zero_entry is a integer scalar, then change the allocation to
tf = false(non_zero_entry,1);
Second assumption non_zero_entry is a vector then change
tf = tf(randperm(non_zero_entry));
to
tf = tf(randperm(end))
Similarly correction must be applied for the earlier instruction
tf(1:round(training_data*end)) = true

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by