Filter löschen
Filter löschen

n! permutation matrices

13 Ansichten (letzte 30 Tage)
Daniel Brower
Daniel Brower am 11 Dez. 2019
Beantwortet: Bandar am 11 Dez. 2019
I need to generate 24 (4!) distict permutation 4x4 matrices. How would I do that?
The first one would need to be the identity matrix =
[1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1]
Each other would be variations of the identity matrix having different order of rows of the identity matrix. There would be exactly 24 (4!) different possible distict variations of the identity matrix, counting the identity matrix.

Antworten (4)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH am 11 Dez. 2019
identity=eye(4);
total=perms(1:4);
for k=1:24
matrixperm=identity(total(k,:),:)
end
  1 Kommentar
Daniel Brower
Daniel Brower am 11 Dez. 2019
How would I put each matrixperm in a cell or array?

Melden Sie sich an, um zu kommentieren.


Fabio Freschi
Fabio Freschi am 11 Dez. 2019
Is this what you wish?
% identity matrix
A = eye(4);
% permuatations
idx = perms(1:4);
% all matrices in a cell array
B = arrayfun(@(i)A(idx(i,:),:),1:24,'UniformOutput',false)
  1 Kommentar
Daniel Brower
Daniel Brower am 11 Dez. 2019
Is there a way to change the cells in B into matrices? I am not familiar with using cells.

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 11 Dez. 2019
Bearbeitet: Stephen23 am 11 Dez. 2019
A purely numeric solution without loops:
>> I = eye(4);
>> M = reshape(I(:,flipud(perms(1:4)).'),4,4,24)
M =
ans(:,:,1) =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
ans(:,:,2) =
1 0 0 0
0 1 0 0
0 0 0 1
0 0 1 0
ans(:,:,3) =
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
ans(:,:,4) =
1 0 0 0
0 0 0 1
0 1 0 0
0 0 1 0
ans(:,:,5) =
1 0 0 0
0 0 1 0
0 0 0 1
0 1 0 0
ans(:,:,6) =
1 0 0 0
0 0 0 1
0 0 1 0
0 1 0 0
... more here
ans(:,:,21) =
0 0 1 0
0 1 0 0
0 0 0 1
1 0 0 0
ans(:,:,22) =
0 0 0 1
0 1 0 0
0 0 1 0
1 0 0 0
ans(:,:,23) =
0 0 1 0
0 0 0 1
0 1 0 0
1 0 0 0
ans(:,:,24) =
0 0 0 1
0 0 1 0
0 1 0 0
1 0 0 0

Bandar
Bandar am 11 Dez. 2019
You may consider creating multidemintional matrix as follows:
I=eye(4);
pr=perms(1:4);
A=zeros(4,4,24);
for i=1:24
A(:,:,i) = I(pr(i,:),:);
end
A

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by