Sort elements of matrices
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Adam Danz
am 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.
Akzeptierte Antwort
David Hill
am 23 Sep. 2020
Bearbeitet: David Hill
am 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)
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 Kommentare
David Hill
am 23 Sep. 2020
How can the different groups have columns with a different number of rows? This does not match your example.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!