matlab - 如何在不耗尽内存的情况下在 Matlab 中使用 randsample?

标签 matlab random

我要 sample 无更换 m 1之间的整数和 n在 Matlab 中,其中

m=10^6;
p=13^5;
n=p*(p-1)/2;
我曾尝试使用 randsample如下
random_indices_pairs=randsample(n,m);
但是,我遇到了一个内存问题
    Error using zeros
    Requested 1x68929060278 (513.6GB) array exceeds maximum array size preference. Creation of arrays greater than this
    limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more
    information.

Error in randsample (line 149)
                x = zeros(1,n); % flags
有没有办法避免这种情况?这里的问题是由于 n很大。

最佳答案

randperm 的两输入版本相当于 randsample无需更换,并且没有内存问题:

random_indices_pairs = randperm(n, m);

关于matlab - 如何在不耗尽内存的情况下在 Matlab 中使用 randsample?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63175026/

相关文章:

c++ - OpenCV 中的连接组件

Javascript 从数组中获取随机变量,但每个变量只有一次

performance - 如何重构这个 Haskell 随机字节输出器?

vb.net - 为什么随机数序列不一样?

performance - 为什么这个乘法这么慢

matlab - 查找 MAtrix 中具有 NAN 值的行并将其删除

matlab - 如何在 Matlab 中找到二维矩阵排列后的映射

matlab - 为什么使用 DFT 的相关性会给出不直观的结果?

java - 柯西随机数生成

c# - 从字符串数组中随机选择,不重复