How to get all possible permutations in binary format?

조회 수: 5 (최근 30일)
lilly lord
lilly lord 2022년 4월 29일
댓글: lilly lord 2022년 5월 2일
Hello, I want to get possible permutation of the vector v which actually contains a boolean values. Suppose the first permutation is v=[b2 b3 b1] . So it put the second column first then third column and finally the first colum and convert it into decimals
B=[0,1,0;0,0,1;1,1,0;0,1,1;1,0,1;1,0,0];
b1=B(:,1);
b2=B(:,2);
b3=B(:,3);
v=[b1 b2 b3];
c=perms(v);
final =bi2de(c)
  댓글 수: 2
Voss
Voss 2022년 4월 29일
You say v is a vector, but in the code v is a matrix. And it's not clear why in the code you split the columns of matrix B into three separate variables b1, b2, and b3, only to recombine them into matrix v, so that v is the same as B.
B=[0,1,0;0,0,1;1,1,0;0,1,1;1,0,1;1,0,0];
b1=B(:,1);
b2=B(:,2);
b3=B(:,3);
v=[b1 b2 b3];
isequal(B,v)
ans = logical
1
Can you please clarify what exactly you want to do? You want all permutations of what exactly?
lilly lord
lilly lord 2022년 4월 29일
v=[b1 b2 b3] I want to permute v ,for exapmle v=[b1,b3 b2] then v=[b3 ,b2,b1] and by doing so it actually takes the valurs for b1=B(:,1) etc. I need different decimal values after each permutation

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

채택된 답변

DGM
DGM 2022년 4월 29일
편집: DGM 2022년 4월 30일
I'm not sure, but are you thinking of something like this?
B = [0,1,0;0,0,1;1,1,0;0,1,1;1,0,1;1,0,0];
% all permutations of the columns of B
v = B(:,perms(1:size(B,2)));
v = reshape(v.',3,[]).'
v = 36×3
0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 1 0
% convert to dec
c = bi2de(v,'left-msb')
c = 36×1
1 4 4 1 2 2 6 0 1 2
  댓글 수: 3
DGM
DGM 2022년 4월 30일
If you want to organize the results in something other than one long vector, you can represent the results for each permutation as columns of a numeric array:
B = [0,1,0;0,0,1;1,1,0;0,1,1;1,0,1;1,0,0];
v = B(:,perms(1:size(B,2)));
v = reshape(v.',3,[]).';
c = bi2de(v,'left-msb');
c = reshape(c,size(B,1),[]) % one column for each perm
c = 6×6
1 6 1 7 6 0 4 0 7 4 3 3 4 1 6 5 3 2 1 2 5 3 6 4 2 0 7 2 5 5 2 5 2 7 5 0
If you're thinking of making a bunch of dynamically-created variables for each vector, just don't. It would only make things slower and more problematic. For 8 columns, there are only 107520 permutations, so it's still manageable. You may find that the vast majority are nonunique.
lilly lord
lilly lord 2022년 5월 2일
Thank you sir. YOur answer clearify the problem I am facing.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by