【问题标题】:How to shuffle such that two same elements are not together?如何洗牌以使两个相同的元素不在一起?
【发布时间】:2017-10-13 01:04:23
【问题描述】:

我有一个包含多个元素的字符串,有些相同,有些独特。我希望我的代码检查字符串中的每 2 个以下元素,如果它们相等,它应该调用一个函数 ShuffleString,其中输入变量 (randomize) 是字符串本身,它将重新洗牌字符串在一个新的位置。然后,脚本应该重新检查字符串中的每 2 个后续元素,直到没有两个相同的元素出现在彼此旁边。


我做了以下事情:
我的函数文件ShuffleString 工作正常。如前所述,输入变量randomize 包含与MyString 相同的元素,但顺序不同,因为在脚本前面的不相关问题上需要这样做。

function [MyString] = ShuffleString(randomize)
MyString = [];
while length(randomize) > 0
    S = randi(length(randomize), 1);
    MyString = [MyString, randomize(S)];
    randomize(S) = [];
end

脚本没有按预期工作。现在它看起来像这样:

MyString = ["Cat" "Dog" "Mouse" "Mouse" "Dog" "Hamster" "Zebra" "Obama"...
    "Dog" "Fish" "Salmon" "Turkey"];

randomize = MyString;
while(1)
    for Z = 1:length(MyString)
        if Z < length(MyString)
            Q = Z+1;
        end
        if isequal(MyString{Z},MyString{Q})
            [MyString]=ShuffleString(randomize)
            continue;
        end
    end
end

它似乎只是将字符串重新洗牌无数次。这有什么问题,我怎样才能让它发挥作用?

【问题讨论】:

  • 因为您使用的是无限循环? while(1).

标签: matlab loops shuffle


【解决方案1】:

您正在使用一个无限的while 循环,它无法中断,因此它会不断迭代。

这是一个更简单的方法:
使用unique 函数的第三个输出参数来获取数字形式的元素,以便于处理。在其上应用diff 以检查连续元素是否相同。如果出现任何相同的连续元素,diff 的输出将给出至少一个零,当与negated 一起应用时all 将返回true 以继续循环,反之亦然。最后,使用循环后获得的字符串的洗牌索引/数字表示来索引unique 的第一个输出参数(之前计算过)。所以脚本将是:

MyString = ["Cat" "Dog" "Mouse" "Mouse" "Dog" "Hamster" "Zebra" "Obama"...
    "Dog" "Fish" "Salmon" "Turkey"]; %Given string array
[a,~,c] = unique(MyString);%finding unique elements and their indices
while ~all(diff(c))        %looping until there are no same strings together
    c = ShuffleString(c);  %shuffling the unique indices 
end
MyString = a(c);           %using the shuffled indices to get the required string array

对于函数ShuffleString,更好的方法是使用randperm。您的函数版本有效,但它不断改变数组MyStringrandomize 的大小,因此adversely affects the performance and memory usage。这是一个更简单的方法:

function MyString = ShuffleString(MyString)
MyString = MyString(randperm(numel(MyString)));
end

【讨论】:

  • 哇,太棒了!工作完美,正是我想要的,非常感谢!
猜你喜欢
  • 2021-06-22
  • 2021-10-27
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
相关资源
最近更新 更多