Sort cell array with matrix

1 次查看(过去 30 天)
Hayk Gevorgyan
Hayk Gevorgyan 2018-1-22
回答: Voss 2023-1-3
Hello,
I have a 1x3x70 cell array with text, double and a 1x100 matrix How can I sort this elements according to the second column (double)
The cell array look like this :
myCellTest{1,1,62} =
./ref/image62.gif
myCellTest{1,2,62} =
195.7271
myCellTest{1,3,62} =
Columns 1 through 12
211.0024 209.3442 205.5262 200.4445 197.9091 195.4380 191.2694 185.6475 175.9005 162.2899 147.4110 135.5913
Columns 13 through 24
123.7942 113.2166 104.4844 97.4166 87.1321 76.0592 72.4983 70.5762 68.9638 67.9117 66.9104 66.3702
Columns 25 through 36
67.0671 67.0075 67.1863 68.5930 70.4060 72.5328 75.2728 80.2309 87.2067 93.6483 98.7370 109.8089
Columns 37 through 48
118.3427 125.1599 135.2812 144.6271 156.1570 167.2154 180.6793 193.9407 209.0670 222.8004 235.5865 242.8353
Columns 49 through 60
242.9979 240.0750 235.1361 227.0683 212.7346 190.9057 170.2058 149.3452 135.6503 124.3543 114.9087 111.2520
Columns 61 through 72
100.2646 93.4345 87.6926 83.7377 78.6003 76.5506 73.4302 72.0139 72.6154 79.0569 89.9889 93.9415
Columns 73 through 84
81.7435 80.2247 86.0058 92.0869 94.5304 94.3663 97.5141 101.2423 105.6882 109.6586 115.4123 122.1147
Columns 85 through 96
129.2749 137.2771 145.0965 153.4829 161.0745 169.4019 176.4086 183.5756 189.6444 195.1666 201.4373 206.6495
Columns 97 through 100
212.0024 212.7205 210.4661 211.0024
-----------------------------------------------------------------
myCellTest{1,1,63} =
./ref/spoon-1.gif
myCellTest{1,2,63} =
277.1378
Thanks Best regards

回答(2 个)

Jiri Hajek
Jiri Hajek 2023-1-2
Hi, in order to achieve what you need, you have to perform few steps.
% Extract numeric vector containing just the value used for sorting
unsortedDataVector = [myCellTest{1,2,:}];
% Sort it and keep the indexes
[sortedDataVector,sortIndexes] = sort(unsortedDataVector);
% Use the ordering information for whatever purpose, e.g. to create a new,
% sorted cell array:
mySortedCellTest = cell(size(myCellTest)); % preallocate
[mySortedCellTest{1,1,:}] = deal(myCellTest{1,1,sortIndexes});
[mySortedCellTest{1,2,:}] = deal(myCellTest{1,2,sortIndexes});
[mySortedCellTest{1,3,:}] = deal(myCellTest{1,3,sortIndexes});

Voss
Voss 2023-1-3
[~,idx] = sort([myCellTest{:,2,:}];
mySortedCellTest = myCellTest(:,:,idx);

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by