How to combine two matrices in the form of ordered pairs?

23 visualizzazioni (ultimi 30 giorni)
I have two matrices A and B which are for example
A=[1 2 3;2 3 1; 3 1 2] and B=[1 2 3;3 1 2;1 3 2]
And I would like to combine these matrices so that every other column is from A and every other is from B. So the answer should be matrix such that each entry is in the form of ordered pair or there is no space between them i.e.,
[ (1,1) (2,2) (3,3);(2,3) (3,1) (1,2);(3,1) (1,3) (2,2)]
  2 Commenti
Guillaume
Guillaume il 25 Gen 2018
  • matlab does not have any concept of matrix of pairs. Each element of a matrix is always a single number. You could model pairs by adding an extra dimension or by using cell arrays of 1x2 matrices. Which to use will very much depends on what you want to do with them
  • matrices don't have space, whatever you mean by that. Numbers are always stored contiguously in memory.
As per Jan's answer you really need to be clearer about what you want and not use terms and notation that have no meaning in matlab.
Steven Lord
Steven Lord il 25 Gen 2018
Another alternative would be to store the pairs as a complex matrix.
C = A+1i*B

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 25 Gen 2018
It would seem that your ordered pairs are actually two digits integers formed by putting the first element as decades, and the second as units. If that is the case, this is trivially achieved by:
A = [1 2 3; 2 3 1; 3 1 2]
B = [1 2 3; 3 1 2; 1 3 2]
C = 10*A + B
  4 Commenti
Ammy
Ammy il 26 Gen 2018
A = [1 2 3; 2 3 1; 3 1 2 ]
B = [1 2 3; 3 1 2; 1 3 2 ]
C = 10*A + B
C =
11 22 33
23 31 12
31 13 22
For the reverse process what will I do that is
If I have C , How can I get A and B
Guillaume
Guillaume il 26 Gen 2018
I have really no idea what you are doing, pairing your numbers as 2 digits integer sounds like a waste of time. You would probably be better off doing as Jan suggested, pairing them as pages of a 3D matrix. In that case it is trivial to go from A and B to C and back:
C = cat(3, A, B);
A = C(:, :, 1);
B = C(:, :, 2);
The above will work whatever the size of A and B and whatever the magnitude of the numbers
If you insist on pairing them as integers, which only works for integers 0-9, then:
A = floor(C/10);
B = mod(C, 10);

Accedi per commentare.

Più risposte (1)

Jan
Jan il 25 Gen 2018
With
[(1,1) (2,2) (3,3);(2,3) (3,1) (1,2);(3,1) (1,3) (2,2)]
you introduce a notation without defining it. There is no such object in Matlab and it is not clear what this "combination" should be. "no space between them" is not explained also.
Instead of inventing new classes of variables, it is much better to use the possibilities, which are offered by the programming language already. With
A = [1 2 3; 2 3 1; 3 1 2]
B = [1 2 3; 3 1 2; 1 3 2]
C = cat(3, A, B)
You concatenate the matrices along the 3rd dimension. Then your data in the parenthesis, e.g. "(1,1)" can be obtained by:
C(1, 1, :)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by