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
0 个评论
回答(1 个)
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!