When using datasample for random selection how to store the nonslected values in a separate array?

2 次查看(过去 30 天)
I have the following array:
A = {'E_1'; 'E_2'; 'E_3'; 'E_4'; 'E_5'; 'E_6'; 'E_7'; 'E_8'; 'E_9'; 'E_10'; 'E_11'; 'E_12'};
and I want to sample randomly from it 10 samples. To these 10 samples I assign values randomly between 0.8 and 1.
nsamples = 7;
for i=1:nsamples
y(:,i) = datasample(A,10,'Replace',false);
y{1,i} = struct(y{1,i},[0.80 + 0.2*rand(1)]);
y{2,i} = struct(y{2,i},[0.80 + 0.2*rand(1)]);
y{3,i} = struct(y{3,i},[0.80 + 0.2*rand(1)]);
y{4,i} = struct(y{4,i},[0.80 + 0.2*rand(1)]);
y{5,i} = struct(y{5,i},[0.80 + 0.2*rand(1)]);
y{6,i} = struct(y{6,i},[0.80 + 0.2*rand(1)]);
y{7,i} = struct(y{7,i},[0.80 + 0.2*rand(1)]);
y{8,i} = struct(y{8,i},[0.80 + 0.2*rand(1)]);
y{9,i} = struct(y{9,i},[0.80 + 0.2*rand(1)]);
y{10,i} = struct(y{10,i},[0.80 + 0.2*rand(1)]);
%y{10,i} = struct(y{10,i},[0.80 + 0.2*rand(1)]);
end
Then the samples that were not selected from array A I want to store them separately. What command should I use to separate the 2 that were not selected and to create for them a new array?
Please let me know if the question needs to be clarified?
Thanks, Elitsa

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2014-3-17
编辑:Andrei Bobrov 2014-3-17
A = regexp(sprintf('E_%d ',1:12),'\w*','match');
m = numel(A);
n = 7; % nsamples
k = 10;
[~,ii] = sort(rand(m,n));
y = cellfun(@(x,z)struct(x,z),A(ii(1:k,:)),num2cell(.8+.2*rand(k,n)),'un',0);
  1 个评论
elidzh5
elidzh5 2014-3-17
Hi Andrei,
Thank you for the answer.
The samples from A that were not selected (every time I have two that are left out), I want to store them separately. What command should I use to separate the 2 that were not selected and to create for them a new array? Because I also want to assign for them another randomly choosen values? Now I have y (10x7) for the 10 that are raddomly chosen but also i want to have the two left out (2x7). I don't know how to do that?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by