Sort elements of matrices

2 visualizaciones (últimos 30 días)
Sim
Sim el 23 de Sept. de 2020
Editada: Sim el 23 de Sept. de 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.
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 comentarios
Adam Danz
Adam Danz el 23 de Sept. de 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.
Sim
Sim el 23 de Sept. de 2020
Editada: Sim el 23 de Sept. de 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 :)

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 23 de Sept. de 2020
Editada: David Hill el 23 de Sept. de 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)
a=ismember(A{1},A{2});
A{1}=reshape([sort(A{1}(~a));A{1}(a)],4,[])';
for k=2:length(A)-1
b=ismember(A{k},A{k-1});
a=ismember(A{k},A{k+1});
A{k}=reshape([A{k}(b);sort(A{k}(~(a+b)));A{k}(a)],4,[])';
end
A{end}=reshape([A{end}(ismember(A{end},A{end-1}));sort(A{end}(~ismember(A{end},A{end-1})))],4,[])';
Changed the input to cell of matrices:
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']
};
  3 comentarios
David Hill
David Hill el 23 de Sept. de 2020
How can the different groups have columns with a different number of rows? This does not match your example.
Sim
Sim el 23 de Sept. de 2020
Editada: Sim el 23 de Sept. de 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)..... :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by