美文网首页
从已有数组中生成随机不重复数组

从已有数组中生成随机不重复数组

作者: VivaVida | 来源:发表于2022-05-02 10:20 被阅读0次

    生成随机不重复数组

    如何从现有的数组中抽取数字并组成数组

    %%%代码%%%

    n=8;%生成数组维度

    % ll 从一个数组中抽取

    hyper=[2,4,10,22,3,44,6];

    ll_2_0=[];

    s=1;

    while  s<n

        ll=randsample(hyper,2);%从现有数组中抽取两个数字

        ll_2_0=[ll_2_0;ll];

        ll_2_0=unique(ll_2_0,'rows');%去重

        s=size(ll_2_0,1);

    end

    ll_2_0=num2cell(ll_2_0,2);%将数组转化为cell存储

    %%%从两个数组中抽取

    ll_1_2=[];

    s=1;

    while  s<n

        ll_1=randsample(hyper,1);

        ll_2=randsample(nonhy,2);

        ll=[ll_1,ll_2];

        ll=perms(ll);

        ll=ll(randperm(size(ll,1),1),:);

        ll_1_2=[ll_1_2;ll];

        ll_1_2=unique(ll_1_2,'rows');

        s=size(ll_1_2,1);

    end

    ll_1_2=num2cell(ll_1_2,2);

    相关文章

      网友评论

          本文标题:从已有数组中生成随机不重复数组

          本文链接:https://www.haomeiwen.com/subject/lrppyrtx.html