Creating permutations from rows of a matrix

I have a matrix
X=[2-i 3+4i 5+7i; 7+8i 4-9i 7+9i; 2+i 4+8i 3-9i]
I would like to generate the following matrix (P) from X. Matrix P has six rows in total (3!), where by each row is composed of all the rows in X combined in a different order.
P=[2.0000 + 1.0000i 4.0000 + 8.0000i 3.0000 - 9.0000i 7.0000 + 8.0000i 4.0000 - 9.0000i 7.0000 + 9.0000i
2.0000 + 1.0000i 4.0000 + 8.0000i 3.0000 - 9.0000i 2.0000 - 1.0000i 3.0000 + 4.0000i 5.0000 + 7.0000i
7.0000 + 8.0000i 4.0000 - 9.0000i 7.0000 + 9.0000i 2.0000 + 1.0000i 4.0000 + 8.0000i 3.0000 - 9.0000i
7.0000 + 8.0000i 4.0000 - 9.0000i 7.0000 + 9.0000i 2.0000 - 1.0000i 3.0000 + 4.0000i 5.0000 + 7.0000i
2.0000 - 1.0000i 3.0000 + 4.0000i 5.0000 + 7.0000i 2.0000 + 1.0000i 4.0000 + 8.0000i 3.0000 - 9.0000i
2.0000 - 1.0000i 3.0000 + 4.0000i 5.0000 + 7.0000i 7.0000 + 8.0000i 4.0000 - 9.0000i 7.0000 + 9.0000i
Columns 7 through 9
2.0000 - 1.0000i 3.0000 + 4.0000i 5.0000 + 7.0000i
7.0000 + 8.0000i 4.0000 - 9.0000i 7.0000 + 9.0000i
2.0000 - 1.0000i 3.0000 + 4.0000i 5.0000 + 7.0000i
2.0000 + 1.0000i 4.0000 + 8.0000i 3.0000 - 9.0000i
7.0000 + 8.0000i 4.0000 - 9.0000i 7.0000 + 9.0000i
2.0000 + 1.0000i 4.0000 + 8.0000i 3.0000 - 9.0000i]

2 commentaires

Walter Roberson
Walter Roberson le 18 Juil 2020
You happen to be using a square matrix, X -- it is 3 x 3. Will your input X always be 3 x 3? Will your input X always be square? If X were 3 x 4 what would the expected output be like?
You can write it up like a prototype: if the original matrix were [11 12 13 14; 21 22 23 24; 31 32 33 34] then what would the expected output be?
Roland Niwareeba
Roland Niwareeba le 18 Juil 2020
X will not always be Square. Forexample
if
X=[1 4 3 2; 2 9 7 6];
then
P =
[2 9 7 6 1 4 3 2
1 4 3 2 2 9 7 6];
The number of rows here in P is 2! obtained through possible permutations from the X rows.
if X= [11 12 13 14; 21 22 23 24; 31 32 33 34];
then P =
31 32 33 34 21 22 23 24 11 12 13 14
31 32 33 34 11 12 13 14 21 22 23 24
21 22 23 24 31 32 33 34 11 12 13 14
21 22 23 24 11 12 13 14 31 32 33 34
11 12 13 14 31 32 33 34 21 22 23 24
11 12 13 14 21 22 23 24 31 32 33 34
Number of rows in P is 3! which is 6. Each row contains rows in X arranged in a different order.

Connectez-vous pour commenter.

 Réponse acceptée

Bruno Luong
Bruno Luong le 18 Juil 2020
Modifié(e) : Bruno Luong le 18 Juil 2020
This question is almost like what you ask in this thread
Only the formating of the input is different (in one case you split the sequences in different variables, in other case they are grouped as rows of an array).
The difference is not big enough the create 2 separate threads IMO, and you better think finding solution as combining of separate smaller steps if smaller problem.
% Input
X = [2-i 3+4i 5+7i; 7+8i 4-9i 7+9i; 2+i 4+8i 3-9i];
P = cell2mat(perms(num2cell(X,2)))
Here
num2cell(X,2)
and
{X1 X2 X3} % on the answer of other question
return the same cell for two different input storage schemes. You might reduce your question to this simply formatage.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 18 Juil 2020
Modifié(e) : Walter Roberson le 18 Juil 2020
idx = reshape(nchoosek(1:size(X,1),2).',[],1);
P = X(idx,:);

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by