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

22 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2018-1-25
  • 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 2018-1-25
Another alternative would be to store the pairs as a complex matrix.
C = A+1i*B

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2018-1-25
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 个评论
Ammy
Ammy 2018-1-26
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 2018-1-26
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);

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2018-1-25
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, :)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by