我怎样才能 select 从 MATLAB 中的几个矩阵中随机抽样?

How can I select random samples from several matrices in MATLAB?

使用 MATLAB,我有几个 8760x1 矩阵。我需要随机 select 来自第一个矩阵的一些数据,然后 select 来自其他矩阵的数据,但是与从第一个矩阵获得的位置相同的数据,即

data1 = [a b c d e];
data2 = [f g h i j];

我数据采样[a c d],然后我需要select [f h i],按这个顺序,给我:

out1 = [a c d]
out2 = [f h i]

datasample 是最好的工具吗?或者我该怎么做? 谢谢

mat1 = [4, 3, 5, 4];
mat2 = [1, 1, 2, 2];

s = size(mat1);
[v] = randperm(s(2), 2); <--- remember the indices 


ret = mat1(v);
ret = [ret; mat2(v)];
enter code here


out1 = ret(1, 1:end);
out2 = ret(2, 1:end);

编辑

[v] is a 1/0 vector which represents the places chosen to pick from values. 
now to choose from the data left, we need to extract the data not picked and  
pick from it.

v2 = (1-v);
TempMat = Mat(v2);

TempMat is the remaining data not picked in first place.

我相信下面的解决方案将满足您的需求。在向量 k2 中,您将有 n 个不同的索引,可用于您想要的矩阵。

k1=randperm(length(data1)); 
k2=k1(1:n)    % row of n samples
out1=data1(k2);
out2=data2(k2);

Datasample 会很好用,只要你使用 non-replacing 形式(我猜你不想重复输出。如果你同意,那么忽略 'Replace' 标志).索引输出也将是未排序的,因此您可以完美地将其用于 data2:

data1 = [a b c d e];
data2 = [f g h i j]
[out1,idx] = datasample(data1,k,'Replace',false);
out2=data2(idx);

我看到你也"need to randomly extract more 1500 only of the remaining 2760 and then, the remaining 1260 on a third vector.",你可以使用idx信息忽略那个集合:

idx_notused=setdiff(1:size(data1,1),idx); %finds all positions not selected previously
[out1_v2,idx2] = datasample(data1(idx_notused),k,'Replace',false); %k=1500
idx2=idx_notused(idx2); %so it maps with the original data
out2_v2=data2(idx2);

%and again for the remaining 1260:
idx_remaining=setdiff(1:size(data1,1),[idx idx2]);
out1_v3=data1(idx_remaining);
out2_v3=data2(idx_remaining);

对于不重复的抽样,您可以使用 randperm,您从 'chunks' 中的相同数据重新抽样 1500 个左右的事实不会改变您抽样的基本事实重复。所以,你只需要重新排序所有数据,然后将其重塑为你想要的大小:

data1 = ('abcdewryt').';
data2 = ('fghijvbnm').';
k = 2; % samlpe size
N = 3; % no. of times to resample
rand_ind = randperm(size(data1,1)); % reorder all your data
out1 = reshape(data1(rand_ind(1:k*n)),[k,n]); % extract output from data1 in a shuffled order
out2 = reshape(data2(rand_ind(1:k*n)),[k,n]); % extract output from data2 in a shuffled order
left1 = data1(rand_ind(k*n+1:end)); % all what's left in data1
left2 = data1(rand_ind(k*n+1:end)); % all what's left in data2

现在您在 out 的每一列中都有来自数据的样本,并且您有 N 列用于对数据重新采样 N 次。原始向量中剩下的所有内容都在 left 中。

结果示例:

out1 =
ywb
det
out2 =
nvg
ijm
left1 =
a
r
c
left2 =
a
r
c