sorting cell arrays based on nested cell array?
이전 댓글 표시
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
Jan
2021년 3월 17일
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
2021년 3월 17일
편집: Mithushan Kanthasamy
2021년 3월 17일
채택된 답변
추가 답변 (1개)
Purushothaman K
2021년 3월 17일
0 개 추천
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
Mithushan Kanthasamy
2021년 3월 17일
편집: Mithushan Kanthasamy
2021년 3월 17일
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!