Matlab 如何重新排列数字列表,使每N个数字都不重复?
所以我有一个190个数字的列表,范围从1:19(每个数字重复10次),我需要一次采样10个。在每10个样本中,我不希望数字重复,我尝试加入一个while循环,但计算时间太长了。到目前为止,我已经到了可以生成数字并查看每个子集中是否有重复的地步。有什么想法吗Matlab 如何重新排列数字列表,使每N个数字都不重复?,matlab,sorting,Matlab,Sorting,所以我有一个190个数字的列表,范围从1:19(每个数字重复10次),我需要一次采样10个。在每10个样本中,我不希望数字重复,我尝试加入一个while循环,但计算时间太长了。到目前为止,我已经到了可以生成数字并查看每个子集中是否有重复的地步。有什么想法吗 N=[]; for i=1:10 N=[N randperm(19)]; end B=[]; for j=1:10
N=[];
for i=1:10
N=[N randperm(19)];
end
B=[];
for j=1:10
if length(unique(N(j*10-9:j*10)))<10
B=[B 1];
end
end
sum(B)
您希望以10块为单位随机抽取数字1:19,不重复。Matlab函数“randsample”有一个可选的“replacement”参数,如果不想重复,可以将该参数设置为“false”。例如:
N = [];
replacement = false;
for i = 1:19
N = [N randsample(19,10,replacement)];
end
这将生成[1,…,19]范围内的19 x 10随机整数矩阵,每列中不重复
编辑:这里有一个解决方案,解决了每个整数[1,…,19]精确出现10次的要求,并且每个列/样本中没有重复:
nRange = 19; nRep = 10;
valueRep = true; % true while there are repetitions
nLoops = 0; % count the number of iterations
while valueRep
l = zeros(1,nRep);
v = [];
for m = 1:nRep
v = [v, randperm(nRange,nRange)];
end
m1 = reshape(v,nRep,nRange);
for n = 1:nRep
l(n) = length(unique(m1(:,n)));
end
if all(l == nRep)
valueRep = false;
end
nLoops = nLoops + 1;
end
result = m1;
对于问题中的参数,大约需要300次迭代才能找到结果。您希望以10块为单位随机抽取数字1:19,不重复。Matlab函数“randsample”有一个可选的“replacement”参数,如果不想重复,可以将该参数设置为“false”。例如:
N = [];
replacement = false;
for i = 1:19
N = [N randsample(19,10,replacement)];
end
这将生成[1,…,19]范围内的19 x 10随机整数矩阵,每列中不重复
编辑:这里有一个解决方案,解决了每个整数[1,…,19]精确出现10次的要求,并且每个列/样本中没有重复:
nRange = 19; nRep = 10;
valueRep = true; % true while there are repetitions
nLoops = 0; % count the number of iterations
while valueRep
l = zeros(1,nRep);
v = [];
for m = 1:nRep
v = [v, randperm(nRange,nRange)];
end
m1 = reshape(v,nRep,nRange);
for n = 1:nRep
l(n) = length(unique(m1(:,n)));
end
if all(l == nRep)
valueRep = false;
end
nLoops = nLoops + 1;
end
result = m1;
对于问题中的参数,大约需要300次迭代才能找到结果。您希望以10块为单位随机抽取数字1:19,不重复。Matlab函数“randsample”有一个可选的“replacement”参数,如果不想重复,可以将该参数设置为“false”。例如:
N = [];
replacement = false;
for i = 1:19
N = [N randsample(19,10,replacement)];
end
这将生成[1,…,19]范围内的19 x 10随机整数矩阵,每列中不重复
编辑:这里有一个解决方案,解决了每个整数[1,…,19]精确出现10次的要求,并且每个列/样本中没有重复:
nRange = 19; nRep = 10;
valueRep = true; % true while there are repetitions
nLoops = 0; % count the number of iterations
while valueRep
l = zeros(1,nRep);
v = [];
for m = 1:nRep
v = [v, randperm(nRange,nRange)];
end
m1 = reshape(v,nRep,nRange);
for n = 1:nRep
l(n) = length(unique(m1(:,n)));
end
if all(l == nRep)
valueRep = false;
end
nLoops = nLoops + 1;
end
result = m1;
对于问题中的参数,大约需要300次迭代才能找到结果。您希望以10块为单位随机抽取数字1:19,不重复。Matlab函数“randsample”有一个可选的“replacement”参数,如果不想重复,可以将该参数设置为“false”。例如:
N = [];
replacement = false;
for i = 1:19
N = [N randsample(19,10,replacement)];
end
这将生成[1,…,19]范围内的19 x 10随机整数矩阵,每列中不重复
编辑:这里有一个解决方案,解决了每个整数[1,…,19]精确出现10次的要求,并且每个列/样本中没有重复:
nRange = 19; nRep = 10;
valueRep = true; % true while there are repetitions
nLoops = 0; % count the number of iterations
while valueRep
l = zeros(1,nRep);
v = [];
for m = 1:nRep
v = [v, randperm(nRange,nRange)];
end
m1 = reshape(v,nRep,nRange);
for n = 1:nRep
l(n) = length(unique(m1(:,n)));
end
if all(l == nRep)
valueRep = false;
end
nLoops = nLoops + 1;
end
result = m1;
对于问题中的参数,大约需要300次迭代才能找到结果。我认为您应该以建设性的方式进行处理
series1=repmat(1:19,1,10),就可以很容易地找到满足您条件的19个组代码>和重新排列=重塑(系列1,10,19)
test=@(x)numel(unique(x))==10
-如果是,则替换列当然,您可能会想出更有效的洗牌或测试方法,我认为您应该以建设性的方式进行
series1=repmat(1:19,1,10),就可以很容易地找到满足您条件的19个组代码>和重新排列=重塑(系列1,10,19)
test=@(x)numel(unique(x))==10
-如果是,则替换列当然,您可能会想出更有效的洗牌或测试方法,我认为您应该以建设性的方式进行
series1=repmat(1:19,1,10),就可以很容易地找到满足您条件的19个组代码>和重新排列=重塑(系列1,10,19)
test=@(x)numel(unique(x))==10
-如果是,则替换列当然,您可能会想出更有效的洗牌或测试方法,我认为您应该以建设性的方式进行
series1=repmat(1:19,1,10),就可以很容易地找到满足您条件的19个组代码>和重新排列=重塑(系列1,10,19)
test=@(x)numel(unique(x))==10
-如果是,则替换列当然,您可能会想出更有效的洗牌或测试方法,这里有一些非常简单的步骤,基本上只需洗牌向量一次,然后获取最后10个唯一值:
v = repmat(1:19,1,10);
v = v(randperm(numel(v)));
[a idx]=unique(v);
result = unique(v);
v(idx)=[];
如果你想做下一步,算法应该相当有效