split dataset with probability weights

2 次查看(过去 30 天)
Hello everyone
i have a dataset like 20 x 6. i want to split dataset (npop1) into two dataset which depend on probabbility 'weights'. the probability comes from random number r1. i can't figure out how to store the rest data into another variable dataset (rw2). I don't use randperm because randperm has no probability function. I will appreciate any help. Thank you.
Sorry for my bad English..
pc = 0.4; %percentage to split data
nfitA = length(fitA);
PC = pc*npop;% make positive interger
r1 = rand (nfitA,1); %random number for weight probability
rw1 = datasample(npop1,PC,'Weights',r1);

采纳的回答

Fabio Freschi
Fabio Freschi 2019-10-15
datasample has a two outputs, where the second is the index to the selected data in your npop1. So:
[rw1,idSelected] = datasample(npop1,PC,'Weights',r1);
idUnselected = setdiff(1:nfitA,idSelected);
rw2 = npop1(idUnselected,:);
  7 个评论
Fabio Freschi
Fabio Freschi 2019-10-15
Got the point! As Jos highlighted, datasample samples with replacement. This means that each sample can be selected more than once. see for example this link.
If you don't want the replacement, you must instruct datasample in this way:
[rw1,idSelected] = datasample(npop1,PC,'Weights',r1,'Replace',false);
You can leave the rest of the code unchanged. Let me know if this fits your initial request.
Genz
Genz 2019-10-15
its work well. i don't realize there was a "replacement".
many thanks sir. GBU.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by