sorting cell arrays based on nested cell array?
Ältere Kommentare anzeigen
I have a 5x5 cell array and the last cell column is nested cell array which contains double and I have to sort the whole cell array based on the double? is there way to do it?
2 Kommentare
Jan
am 17 Mär. 2021
An explicit example of the input data would be useful. Are the elements of the last column all scalar cells?
Data = {1, 2, {3}; ...
4, 5, {6}};
If so, why do you use a nested cell for the last column?
Mithushan Kanthasamy
am 17 Mär. 2021
Bearbeitet: Mithushan Kanthasamy
am 17 Mär. 2021
Akzeptierte Antwort
Weitere Antworten (1)
Purushothaman K
am 17 Mär. 2021
0 Stimmen
I assume that you are trying to sort a cell array based on last column which is again a cell array whose first (and only element) cell value is double.
I believe there may be no inbuilt function which can do this directly. But we can use sortrows function by making some changes to the input cell array.
Using sortrows(cellarray, column) we can sort the given cell array based on a column. But the column should not be a cell array.
Check below link for more information about sortrows function
Let’s take an example of below input cell array
x = {2,2,2,2,{2};1,1,1,1,{1};4,4,4,4,{4};5,5,5,5,{5};3,3,3,3,{3}}
Steps to solve the issue
- Convert the 5th column to numeric
numCol = cellfun(@cell2mat, x(:,5))
2. Modify input array with converted column
x(:,5) = num2cell(numCol)
3. Sort the cell array
sortrows(x,5)
Note: This solution modifies the input cell array as it converts the cell array to cell value.
1 Kommentar
Mithushan Kanthasamy
am 17 Mär. 2021
Bearbeitet: Mithushan Kanthasamy
am 17 Mär. 2021
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!