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

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개)

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

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?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2014년 3월 17일

댓글:

2014년 3월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by