Sort elements of matrices
Show older comments
Hi, do you have any idea on how to sort the elements of 4 matrices (here called "groups"), as shown in the picture?

Some explanation: the system is composed of 4 groups, which can be represented by 4 matrices, with different number of rows but same number of columns (i.e. 5x4, 4x4, 3x4, 2x4). Every element is labelled with a letter and each column contains the same set of letters {'A','B','C','D','E','F','G','H','I','L','M','N','O','P'}. As we can see from the figure, the elements with the same label are connected by a line, forming a sort of chain (please see from left to right or from right to left). Some of these chains stay within each group, while other ones connect different groups.
My goal would be to sort the elements in each group as follows:
- If all the elements with the same label/name (e.g. 'A A A A' or 'N N N N', etc..) lie within the same group, i.e. in the same matrix, but in different rows, sort them in one row.
- If some elements with the same label/name (e.g. 'B B B B' or 'I I I I', etc..) are shared between two different groups/matrices, move those elements towards the "external" rows of the respective matrices, i.e. those rows which are the closest to the neighbouring groups/matrices.
INPUT and OUTPUT:
IN = {
{'A','A','C','A';
'B','B','A','C';
'C','C','E','D';
'D','D','D','I';
'E','E','I','E'}
{'F','F','F','F';
'G','G','G','B';
'H','H','B','M';
'I','I','H','H'}
{'L','L','N','N';
'M','M','M','G';
'N','N','P','L'}
{'O','O','L','O';
'P','P','O','P'}
};
OUT= {
{'A','A','A','A';
'C','C','C','C';
'D','D','D','D';
'E','E','E','E';
'B','B','I','I'}
{'I','I','B','B';
'F','F','F','F';
'H','H','H','H';
'G','G','G','M'}
{'M','M','M','G';
'N','N','N','N';
'L','L','P','L'}
{'P','P','L','P';
'O','O','O','O'}
};
6 Comments
Jon
on 23 Sep 2020
Can you assume that any letter is shared by at most two groups?
Jon
on 23 Sep 2020
If a letter is shared by more than 2 groups then how do you define nearest/external rows?
Adam Danz
on 23 Sep 2020
This looks like homework to me in which case you should show what you've tried so we can help you get over the edge rather than propose the entire solution for you.
Accepted Answer
More Answers (0)
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!