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

时间:2020-07-30 14:11:06

标签: matlab random

我想在Matlab中define(function (require) { 'use strict'; var vue = require('vue'); return vue.component('field-date-input', { template: '<input class="form-control" v-model.lazy="displayValue" type="text">', mixins: [VueFormGenerator.abstractField], mounted: function () { var self = this; $(this.$el).daterangepicker({ autoUpdateInput: false, singleDatePicker: true }, function (start, end) { self.displayValue = formatDate(start); }); var startingValue = formatDate(this.displayValue); if (startingValue !== null) { this.displayValue = startingValue; $(this.$el).data('daterangepicker').setStartDate(startingValue); } }, computed: { displayValue: { get: function () { return formatDate(this.value); }, set: function (newValue) { $(this.$el).val(newValue); if (this.value !== null) $(this.$el).data('daterangepicker').setStartDate(newValue); this.value = formatWebApiDate(newValue); } } } }); m之间的不替换1整数示例中,

n

我尝试如下使用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 很大。

3 个答案:

答案 0 :(得分:3)

randperm的两输入版本等效于tion,无需替换,并且没有内存问题:

randsample

答案 1 :(得分:1)

下面的脚本应该可以满足您的需求。

  • 它首先选择m1范围内的n个随机整数。
  • 然后检查是否有重复的条目
  • 否则,脚本停止
  • 如果条目重复:
    • 经历了所有人
    • 1n之间找到另一个随机数
    • 检查整数数组中是否存在新的随机数
      • 如果这样做,它将找到另一个随机数
      • 如果没有,它将替换数组中的重复项,然后移至下一个重复项
%% Initialize
clearvars;
clc;

m = 10e6;
p = 13e5;
n = p*(p-1)/2;

%% Create m random integers between 1 and n
randomInt = randi(n, m, 1);

%% Find indices where duplicate random integers are
% Find indices of unique values, take the index of the first occurrence
[~, I] = unique(randomInt, 'first');
% Generate an array of all indices
dupIdx = 1:length(randomInt);
% Drop indices which point to the first occurrence of the duplicate
% This leaves indices that point to the duplicate
dupIdx(I) = [];
% Free up some memory
clear I;

if isempty(dupIdx)
    disp('Done!')
else
    % For those indices find another random number, not yet in randomInt
    disp('Found duplicates, finding new random numbers for those')
    counter = 0;
    for ii = dupIdx
        counter = counter + 1;
        disp(strcat("Resolving duplicate ", num2str(counter), "/", num2str(length(dupIdx))))
        dupe = true;
        % While the replacement is already in the randomInt array, keep
        % looking for a replacement
        while dupe
            replacement = randi(n, 1);
            if ~ismember(replacement, randomInt)
                % When replacement is unique in randomInt
                % Put replacement in the randomInt array at the right index
                randomInt(ii) = replacement;
                dupe = false;
            end
        end
    end
end

答案 2 :(得分:1)

基于评论之一(该评论还建议可能的改进)。

text/xml