フィルターのクリア

Shuffle matrix rows so that common elements are in completely different order than in a said row

2 ビュー (過去 30 日間)
Say we have a matrix row 'current_row'
1 2 3
and a matrix A
1 2 5
1 3 6
2 4 3
The task is to re-arrange matrix A so that the common elements with 'current:_row' in each row are in compelely different indices.
One solution would be:
5 1 2
6 3 1
2 3 4
So that
for i=1:size(A,1)
find(current_row == A(i,:))
end
gives always three []'s
  2 件のコメント
Matt J
Matt J 2019 年 4 月 26 日
編集済み: Matt J 2019 年 4 月 26 日
gives always three 0s
You mean gives []'s.

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

採用された回答

Matt J
Matt J 2019 年 4 月 26 日
編集済み: Matt J 2019 年 4 月 26 日
Here is a vectorized implementation of what John described.
[m,n]=size(A);
I=repmat((1:m).',1,n);
B=A; %copy for shuffling
pool=find(any(B==current_row,2));
while ~isempty(pool)
p=numel(pool);
[~,J]=sort(rand(p,n),2);
B(pool,:)=B( sub2ind([m,n], I(pool,:), J) ); %shuffle rows in 'pool'
pool=find(any(B==current_row,2)); %narrow the search
end
result=Anew;

その他の回答 (1 件)

John D'Errico
John D'Errico 2019 年 4 月 26 日
What you are looking for is a variation of derangement - thus a permutation such that no element is in the same location as it started.
A simple solution? Just continue to shuffle each other row until it meets the goal, that no common elements are in the same location as they are in current_row. Stop when it does, and then go on to the next row. WTP?
You can probably improve on that scheme a bit, by finding the elements in common between the two rows. Then first permute them into ANY locations, as long as they are not the same as the current_row. Having done that derangement operation, then permute the other elements into any random permutation.
  1 件のコメント
Pseudoscientist
Pseudoscientist 2019 年 4 月 26 日
Hmm I don't know how long this would take, there are hundreds of thousands of current_rows and a 3x3 for every current row

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

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by