随机播放重复数字的向量,这样数字就不会在MATLAB中重复 [英] Shuffle a vector of repeated numbers so the numbers do not repeat in MATLAB

查看:67
本文介绍了随机播放重复数字的向量,这样数字就不会在MATLAB中重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个脚本,该脚本可以产生一定间隔的重复整数向量,但是现在有一个特定的实例,在该实例中,我需要确保将其改组后,数字不会重复.因此,举例来说,我产生了一个重复1-5、36次的向量,并进行了改组.如何确保改组后没有重复的数字?为了使事情变得更加复杂,我需要产生两个这样的向量,它们在相同的索引处永远不会具有相同的值.例如,对于这些向量,让1:5重复两次,那么这就是我要寻找的内容:

Okay, so I have a script that will produce my vector of repeated integers of a certain interval, but now theres a particular instance where I need to make sure that once it is shuffled, the numbers do not repeat. So for example, I produced a vector of repeating 1-5, 36 times, shuffled. How do I ensure that there are no repeated numbers after shuffling? And to make things even more complex, I need to produce two such vectors that do not ever have the same value at the same index. For example, lets say 1:5 was repeated twice for these vectors, so then this would be what I'm looking for:

v1     v2
4      2
2      4
3      2
5      3
4      5
1      4
5      1
1      5
3      1
2      3

我现在以1个向量为例,只是将其偏移1以创建另一个可以满足要求的向量,但是在我的情况下,这实际上是行不通的,因为我不能系统地将它们那样的依赖.

I made that right now by taking an example of 1 vector and just shifting it off by 1 to create another vector that will satisfy the requirements, but in my situation, that wont actually work because I can't have them be systematically dependent like that.

因此,我尝试了一种递归技术,如果向量没有进行剪切,则脚本会重新开始,并且按预期进行的效果不佳.我达到了最大的递归迭代次数,并且我已经意识到这显然不是可行的方法.还有其他选择吗?

So I tried a recursive technique to make the script start over if the vectors did not make the cut and as expected, that did not go over so well. I hit my maximum recursive iterations and I've realized this is clearly not the way to go. Is there some other alternative?

因此,我在以下代码中找到了一种满足上述条件的方法:

So I found a way to satisfy some of the conditions I needed above in the following code:

a = nchoosek(1:5,2);
b = horzcat(a(:,2),a(:,1));
c = vertcat(a,b);

cols = repmat(c,9,1);
cols = cols(randperm(180),:);

我只需要找到一种方法来洗改cols,该方法也不会在列中强制执行重复数字,例如cols(i,1)〜= cols(i + 1,1)和cols(i,2)〜=cols(i + 1,2)

I just need to find a way to shuffle cols that will also enforce no repeating numbers in columns, such that cols(i,1) ~= cols(i+1,1) and cols(i,2) ~= cols(i+1,2)

推荐答案

这可行,但是对于大型数组来说可能不是很有效:

This works, but it probably is not very efficient for a large array:

a = nchoosek(1:5, 2);
while (any(a(1: end - 1, 1) == a(2: end, 1)) ...
    || any(a(1: end - 1, 2) == a(2: end, 2)))
    random_indices = randperm(size(a, 1));
    a = a(random_indices, :);
end
a

如果您想要更快的处理方法,诀窍是在符合条件的地方按逻辑插入每一行,而不是随机重新排列.例如:

If you want something faster, the trick is to logically insert each row in a place where your conditions are satisfied, rather than randomly re-shuffling. For example:

n1 = 5;
n2 = 9;

a = nchoosek(1:n1, 2);
b = horzcat(a(:,2), a(:,1));
c = vertcat(a, b);
d = repmat(c, n2, 1);
d = d(randperm(n1 * n2), :);

% Perform an "insertion shuffle"
for k = 2: n1 * n2

    % Grab row k from array d.  Walk down the rows until a position is
    % found where row k does not repeat with its upstairs or downstairs
    % neighbors.
    m = 1;
    while (any(d(k,:) == d(m,:)) || any(d(k,:) == d(m+1,:)))
        m = m + 1;
    end

    % Insert row k in the proper position.
    if (m < k)
        ind = [ 1: m  k  m+1: k-1   k+1: n1 * n2 ];
    else
        ind = [ 1: k-1   k+1: m  k  m+1: n1 * n2 ];
    end

    d = d(ind,:);
end
d

这篇关于随机播放重复数字的向量,这样数字就不会在MATLAB中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆