Permutation/shuffling of number sets

조회 수: 2 (최근 30일)
Felix
Felix 2011년 3월 15일
I have two sets of numbers, e.g.,
[A1 A2 A3]
and
[B1 B2 B3].
Now I want to swap some of them between the two sets, but the numbers should always stay at the same position.
Some possibilities would be: [B1 A2 A3] and [A1 B2 B3];
or
[A1 B2 B3]
and
[B1 A2 A3].
in case of 2x3 numbers there are
  • 1(intitial position, would be the same as swapping all three)
  • 3(single swaps)
  • 3(double swaps)
  • = 7 possibilites.
It's basically very easy, I probably explained it too complicated. But I cannot figure out how to produce this with perms or some other function. it would already be very helpful just to get a matrix like
[000
001
010
100
011
101
110]
for any size of n, which I could then use as index 1 means swapping 0 means change nothing.

채택된 답변

Doug Hull
Doug Hull 2011년 3월 15일
A = [1 2 3];
n = length(A);
swapIndicies = dec2bin(0:(n^2)-2);
numericSwapIndicies = (swapIndicies == '1')
  댓글 수: 2
Felix
Felix 2011년 3월 16일
how can I undo 'accept answer'?
I just found out this solution gives wrong results for n>6
Jan
Jan 2011년 3월 16일
@Felix: You can ask files@mathworks.com for un-accepting the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Matt Fig
Matt Fig 2011년 3월 15일
Here is another method, using NPERMUTEK:
P = logical(npermutek([0 1],length(A)));
I = ceil((find(P))/size(P,1));
Ar = A(ones(1,size(P,1)),:);
Br = B(ones(1,size(P,1)),:);
Ar(P) = B(I)
Br(P) = A(I)

Sean de Wolski
Sean de Wolski 2011년 3월 15일
C = [A B];
idx = randperm(numel(C));
new_matrix = C(idx(1:3));
  댓글 수: 1
Felix
Felix 2011년 3월 15일
ok this produces one result, but I want all possible permutations. thanks anyway

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by