How permutate 2-by-2 matrices in a single matrix

Hello everyone
I have three matrices as:
a = [1,2;3,4]; b = [2,5;1,6]; c = [3,7;2,4];
and collect them in a single M matrix as, M = [a,b,c]. Now I want to have permutation of these three matrices in M_P matrix as:
M_P = [a b c;
a c b;
b a c;
b c a;
c a b;
c b a].
When I use M_P = perms(M), I could not obtain the [M_P] written above. it permutation all the value inside the matirices, however I just want permutation just matrices (a,b,c) then, place the values of matrices. I would appriciate it you can help me
Thanks in advance

 Risposta accettata

a = [1,2;3,4]; b = [2,5;1,6]; c = [3,7;2,4];
abc= {a,b,c};
abc=perms(abc)
abc = 6×3 cell array
{2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double}
abc=cell2mat(abc)
abc = 12×6
3 7 2 5 1 2 2 4 1 6 3 4 3 7 1 2 2 5 2 4 3 4 1 6 2 5 3 7 1 2 1 6 2 4 3 4 2 5 1 2 3 7 1 6 3 4 2 4 1 2 3 7 2 5 3 4 2 4 1 6

3 Commenti

Peiman Khandar
Peiman Khandar il 29 Ott 2022
Modificato: Peiman Khandar il 29 Ott 2022
Thank you so much for you great answer. You helped me a lot. However, I encounter with another problem now how can do mathematic operation on them. For example:
2*(abc(1,1))
or cos(2*(abc(1,1)))
since this is a cell array it did not let to do any operation on them
Thanks in advance
Take a look at cellfun
It's funny that you ask to create data in format that you don't know how to work with. It is better that you don't jump quickly and think more about the data structure that you know how to manipulate downstream.
Might be what you need is 4D array and not a 2D array of all permulations clump together.
I see. So in that case are you recommending an otherway to obtain my permutation function cause I need to do mathematical operations on it

Accedi per commentare.

Più risposte (1)

Rik
Rik il 28 Ott 2022
Modificato: Rik il 28 Ott 2022
You're close. You just need to store a, b, and c in a cell array and use perms to create indices.
The benefit of this approach is that you can extend it to more or fewer elements (although you will run into memory constraints fast with a larger number of matrices).
a = [1,2;3,4]; b = [2,5;1,6]; c = [3,7;2,4];
collation = {a,b,c};
inds = perms(1:numel(collation))
inds = 6×3
3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3
M_P = collation(inds)
M_P = 6×3 cell array
{2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double} {2×2 double}
M_P = cell2mat(M_P)
M_P = 12×6
3 7 2 5 1 2 2 4 1 6 3 4 3 7 1 2 2 5 2 4 3 4 1 6 2 5 3 7 1 2 1 6 2 4 3 4 2 5 1 2 3 7 1 6 3 4 2 4 1 2 3 7 2 5 3 4 2 4 1 6

7 Commenti

Dear Rik
Thank you so much for you nice comment. You were a great help me a lot.
However, I have another challenging in my calculations.
How can do mathematic operations on them. For example:
2*(abc(1,1))
since this is a cell array it did not let to do any operation on them
Thanks in advance
What do you mean by that operation? What is abc(1,1)?
I mean 2 times the first row and first column of overall matrix [abc] where it contains a matrix
Bruno Luong
Bruno Luong il 29 Ott 2022
Modificato: Bruno Luong il 29 Ott 2022
"I mean 2 times the first row and first column of overall matrix [abc] where it contains a matrix"
Then litterally it is 2*abc(1,1), that means 2*3 = 6
but I guess this is NOT what you want. Describtion verbally and innacurately is the last thing you would do to ask question.
Yes you are right I cannot explain explicitly. The abc matrix containt rows and column where it contains matrices. Now I want to multply 2 to first row first column of matix abc which is:
[3 7;2 4]. However, I cannot write code for that since it is a cell array.
2.*(abc(1,1))
a = [1,2;3,4]; b = [2,5;1,6]; c = [3,7;2,4];
abc= {a,b,c};
abc=perms(abc);
2*abc{1,1}
ans = 2×2
6 14 4 8
Peiman Khandar
Peiman Khandar il 29 Ott 2022
Modificato: Peiman Khandar il 29 Ott 2022
Thanks a lot Bruno (sorry for confusion)

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by