# Random shuffling while reduce the identical pairs

2 ビュー (過去 30 日間)
J T 2020 年 3 月 3 日
コメント済み: Walter Roberson 2020 年 3 月 3 日
Hello,
I have a 50-by-1 array like this:
P = [4 5 5 5 5 5 5 5 5 9 9 9 9 9 9 9 27 27 27 35 40 40 40 40 4 5 5 5 5 5 5 5 5 5 9 9 9 9 9 9 27 27 27 35 35 40 40 40 27 9]';
P should be split into two 25-by-1 arrays A and B after shuffling, and, I want to efficiently shuffle P such that the two children arrays, A and B, consider pairing A(i) and B(i) for i = 1:25, there is the least amout of A(i) == B(i).
Could someone help me out here? Thank you very much!

サインインしてコメントする。

### 採用された回答

Walter Roberson 2020 年 3 月 3 日
tries = 1000;
NB = length(B);
[~,perms] = sort(rand(tries, NB),2);
matchcounts = sum(A == B(perms),2);
bestidx = find(matchcounts == min(matchcounts));
best_shuffles = B(perms(bestidx,:));
You could also sort on matchcounts and take however many that you need.
In my test, I got 11 permutations with only a single match each.
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Image Analyst 2020 年 3 月 3 日
Why do you want to do this? What is the use case?
Walter Roberson 2020 年 3 月 3 日
sP = sort(P(:).');
A = sP(1:end/2);
B = sp(end/2+1:end);
tries = 1000;
NB = length(B);
[~,perms] = sort(rand(tries, NB),2);
matchcounts = sum(A == B(perms),2);
bestidx = find(matchcounts == min(matchcounts));
best_shuffles = B(perms(bestidx,:));
61 entries with overlap 0 in my test.
The above algorithm is not fool-proof. A better algorithm would be to select the value that occurs most often and put all of the copies of it into A, and then to select the second most common value and put all the copies into B, then put all copies of the third most common into A, and so on back and forth, partitioning into non-overlapping sets as much as possible. Ideally you would get A and B that had no overlapping values, in which case every permutation of B would be a total mismatch with every permutation of A.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by