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

조회 수: 22 (최근 30일)
Ammy
Ammy 2018년 1월 25일
댓글: Guillaume 2018년 1월 26일
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