我正在尝试编写一个对数组进行混洗的函数,该函数包含重复元素,但确保重复元素不会彼此太靠近.
这段代码有效但对我来说似乎效率低下:
function shuffledArr = distShuffle(myArr, myDist)
% this function takes an array myArr and shuffles it, while ensuring that repeating
% elements are at least myDist elements away from on another
% flag to indicate whether there are repetitions within myDist
reps = 1;
while reps
% set to 0 to break while-loop, will be set to 1 if it doesn't meet condition
reps = 0;
% randomly shuffle array
shuffledArr = Shuffle(myArr);
% loop through each unique value, …Run Code Online (Sandbox Code Playgroud)