Shuffle two different lists of numbers in the same way

8 ビュー (過去 30 日間)
Kavorka
Kavorka 2016 年 2 月 13 日
コメント済み: Guillaume 2016 年 2 月 13 日
I want to shuffle two vectors, both with n elements, so that they are shuffled in the same way, i.e. the corresponding elements in each are moved to the same spot. I have tried doing this by using randperm like this:
ix = randperm(n);
ShuffledVector1 = Vector1(ix);
ShuffledVector2 = Vector2(ix);
But the shuffling is not done in the same way, I know because when I run it for Vector1=Vector2, ShuffledVector1 doesn't end up equaling ShuffledVector2. Any advice?
  2 件のコメント
Guillaume
Guillaume 2016 年 2 月 13 日
You're doing something wrong in your testing. If Vector1 == Vector2, then ShuffledVector1 == ShuffledVector2, always (with finite elements).
Jan
Jan 2016 年 2 月 13 日
編集済み: Jan 2016 年 2 月 13 日
@Kavorka: Your assumption must be wrong:
If Vector1=Vector2, ShuffledVector1 doesn't end up
equaling ShuffledVector2.
How do you check this? Perhaps some elements are NaN, which reply FALSE for a comparison NaN==NaN ?

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

回答 (1 件)

Star Strider
Star Strider 2016 年 2 月 13 日
Maybe I’m missing something, but if ‘Vector1’ and ‘Vector2’ are different, ‘ShuffledVector1’ and ‘ShuffledVector2’ would be different as well.
Example 1:
Vector1 = randi(9, 1, 10);
Vector2 = randi(9, 1, 10);
ix = randperm(10);
ShuffeledVector1 = Vector1(ix);
ShuffeledVector2 = Vector2(ix);
Q1 = Vector1 == Vector2; % Not Equal
Q2 = ShuffeledVector1 == ShuffeledVector2; % Not Equal
Example 2:
Vector1 = randi(9, 1, 10);
Vector2 = Vector1;
ix = randperm(10);
ShuffeledVector1 = Vector1(ix);
ShuffeledVector2 = Vector2(ix);
Q1 = Vector1 == Vector2; % Equal
Q2 = ShuffeledVector1 == ShuffeledVector2; % Equal
  2 件のコメント
Kavorka
Kavorka 2016 年 2 月 13 日
I'm not saying the vectors aren't different, I want their order to be the same. It doesn't matter what the elements are, but if you assigned each element a number based on their order 1,2,3,4 then after the shuffle both vectors will have the same order 4,2,3,1 or whatever. If example 2 is true, then the shuffled order is the same and I must be doing something wrong after this operation. I will look again
Guillaume
Guillaume 2016 年 2 月 13 日
Yes, you're doing something wrong. Example 2 is true.
If you can't figure out where the problem is. Post the code you're using to test here.

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

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by