When using datasample for random selection how to store the nonslected values in a separate array?
Show older comments
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
Answers (1)
Andrei Bobrov
on 17 Mar 2014
Edited: Andrei Bobrov
on 17 Mar 2014
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);
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!