Sort elements of matrices

4 views (last 30 days)
Sim on 23 Sep 2020
Edited: Sim on 23 Sep 2020
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:
  1. 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.
  2. 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.
IN = {
OUT= {
Sim on 23 Sep 2020
Oh Yes, I will write my attempts here :)
Meanwhile I accepted the David Hill answer, since it actually does what I asked for in this post :)

Sign in to comment.

Accepted Answer

David Hill
David Hill on 23 Sep 2020
Edited: David Hill on 23 Sep 2020
Might be a better way, but here it is for the example. If shared with multiple groups, as stated above, I don't know what you want.
function A = groupSort(A)
for k=2:length(A)-1
Changed the input to cell of matrices:
IN = {
Sim on 23 Sep 2020
Yes, true, I was just trying to simplify the problem.. and then trying to get the solution to my exact case......Meanwhile, I accepted your answer, thanks a lot.....However, I will try to adapt your solution to the case with different number of rows and I will try to post it here (its gonna take a while, since I am currently multitasking)..... :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!