在 MATLAB 中使用或不使用替换生成采样行矩阵的快速方法
Fast way to generate matrix of sampled rows with or without replacement in MATLAB
我有一些输入行。要执行一些数值统计测试,我需要大量相同大小的样本,这些样本是从输入行中有或没有替换地采样的。我有一些非常简单的代码,可以做到这一点:
inputRow = [1 3 4 2 7 5 8 6];
rowSize = numel(inputRow);
nPermutations = 10;
permutedMatrix = nan(nPermutations,rowSize);
replaceFlag = true;
permutedMatrix(1,:) = inputRow;
for iPerm = 2:nPermutations
permutedMatrix(iPerm,:) = datasample(inputRow,rowSize,'Replace',replaceFlag);
end
我的问题是:是否可以在没有 for 循环的情况下生成所需的矩阵?
希望对您有所帮助,
替换重采样:
input=[2 3 4 2 3 4];
len=size(input,2);
number_of_permutations=10;
rand_idx=randi(len,1,len*number_of_permutations);
permutation_matrix=zeros(len,number_of_permutations);
permutation_matrix(:)=input(rand_idx);
permutation_matrix=permutation_matrix';
这是无替换的重采样
input=[2 3 4 2 3 4];
len=size(input,2);
number_of_permutations=10;
rand_idx=repmat(randperm(len,len),1,number_of_permutations);
permutation_matrix=zeros(len,number_of_permutations);
permutation_matrix(:)=input(rand_idx);
permutation_matrix=permutation_matrix';
我有一些输入行。要执行一些数值统计测试,我需要大量相同大小的样本,这些样本是从输入行中有或没有替换地采样的。我有一些非常简单的代码,可以做到这一点:
inputRow = [1 3 4 2 7 5 8 6];
rowSize = numel(inputRow);
nPermutations = 10;
permutedMatrix = nan(nPermutations,rowSize);
replaceFlag = true;
permutedMatrix(1,:) = inputRow;
for iPerm = 2:nPermutations
permutedMatrix(iPerm,:) = datasample(inputRow,rowSize,'Replace',replaceFlag);
end
我的问题是:是否可以在没有 for 循环的情况下生成所需的矩阵?
希望对您有所帮助,
替换重采样:
input=[2 3 4 2 3 4];
len=size(input,2);
number_of_permutations=10;
rand_idx=randi(len,1,len*number_of_permutations);
permutation_matrix=zeros(len,number_of_permutations);
permutation_matrix(:)=input(rand_idx);
permutation_matrix=permutation_matrix';
这是无替换的重采样
input=[2 3 4 2 3 4];
len=size(input,2);
number_of_permutations=10;
rand_idx=repmat(randperm(len,len),1,number_of_permutations);
permutation_matrix=zeros(len,number_of_permutations);
permutation_matrix(:)=input(rand_idx);
permutation_matrix=permutation_matrix';