随机唯一整数值

时间:2013-11-08 10:50:53

标签: matlab random matrix

是否有randi的替代方案,我需要唯一整数值。使用randi PianoSperimentale矩阵可能包含重复的整数值。

lover_bound = 10;
upper_bound = 180;
steps = 10;
NumeroCestelli = 8; 

livello = [lover_bound:steps:upper_bound];
L = length(livello);
n_c = 500 000
NumeroCestelli = 8

randIdxs = randi([1,L],n_c,NumeroCestelli);
PianoSperimentale = single(livello(randIdxs)); 

替代方案需要快速并支持非常大的矩阵。在过去我使用这个:

[PianoSperimentale] = combinator(L,NumeroCestelli,'c','r');

for i=1:L
    PianoSperimentale(PianoSperimentale==i)=livello(i);
end

但太慢而痛苦。 (见Combinator

2 个答案:

答案 0 :(得分:5)

是的,有:

randsample(10,3)

给出一个从3到10的3个整数的向量,无需替换。

如果您需要矩阵而不是矢量:

matrix = NaN(8,12);
matrix(:) = randsample(1000,numel(matrix));

给出一个8x12的唯一整数矩阵,从1到1000。

函数randsample位于统计工具箱中。如果你没有它,你可以使用randperm代替,如@RodyOldenhuis和@Dan所述(请参阅@ Dan的回答)。

答案 1 :(得分:1)

此外,如果您没有统计工具箱,则可以使用randperm

randperm(m, k) + n - 1

这也会在kn之间为您提供n+m个随机整数而无需替换