Permute/Re​arrange/Sh​uffle the elements of a matrix

조회 수: 3 (최근 30일)
Grace
Grace 2014년 6월 14일
댓글: Grace 2014년 6월 16일
Hi, I have
A=[2 3
4 1
1 4
3 2
5 5];
I want have the all possible arrangements, with a condition,there is no repetition for each column.
How can I do this? Thanks in advance.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2014년 6월 15일
Grace - so you want all 5x2 matrices which have elements 1 through 5 in each column arranged uniquely? Or are the rows fixed and you want all arrangements of the rows? Please provide some of the arrangements from your above example or start with a smaller matrix to make it more clear what you expect.
Grace
Grace 2014년 6월 15일
편집: Grace 2014년 6월 15일
Hi Geoff, yes, I want all 5x2 matrices have elements 1 through 5 in each column arranged uniquely.
Let's take a smaller matrix as an example:
B=[1 1;
2 2]
The matrix above contains 2 rows, the elements are 1 and 2.
The possible arrangements for B are
B1=[1 2;
2 1]
B2=[2 2;
1 1]
B3=[2 1;
1 2]
The arrangements like
[1 1;
1 2]
or
[1 2;
1 2]
which contain repeated elements for each column are not the arrangements that i wish to get.

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

답변 (1개)

Roger Stafford
Roger Stafford 2014년 6월 15일
With n = 5 rows in A you would get 14,400 different B matrices and that is too many to dream up names for, so I have put them all into a single n-by-2-by-(n!)^2 array which I call B. Each slice along the third dimension would be one of your n-by-2 matrices which you called B1, B2, B3, etc.
n = size(A,1);
P = perms(1:n)';
N = size(P,2);
B = zeros(n,2,N,N);
for i1 = 1:N
for i2 = 1:N
B(:,:,i1,i2) = [A(P(:,i1),1),A(P(:,i2),2)];
end
end
B = reshape(n,2,N^2);
  댓글 수: 1
Grace
Grace 2014년 6월 16일
Hi Roger, I can't understand the reshape part, and the output shows me:
>> Untitled
Error using reshape
To RESHAPE the number of elements must not
change.
Error in Untitled (line 42)
B = reshape(n,2,N^2)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by