フィルターのクリア

How can I sort data in cells?

1 回表示 (過去 30 日間)
Janna Hinchliff
Janna Hinchliff 2019 年 2 月 22 日
編集済み: Adam Danz 2019 年 2 月 24 日
I have some sets of data saved in a cell array, where each element in the cell array is another cell array containing 3 elements, i.e.
data = 1x6 cell array
{1 x 3 cell} {1 x 3 cell} {1 x 3 cell} {1 x 3 cell} {1 x 3 cell} {1 x 3 cell}
The 1x3 cells all contain 2 single numbers and one mxn data set. I want to sort the data by the second number in the data set -
dataovenparamcell{j}{2}
such that cells with the same value of the second number are grouped together. How could I do this?
  1 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 22 日
Please illustrate with an example.

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 2 月 22 日
編集済み: Adam Danz 2019 年 2 月 24 日
This solution can be reduced two lines but I added a third so it's easier to understand. Fake data are created at the top.
% Create fake data
data = {
{6 5 rand(3,4)}, {0 2 rand(3,4)}, {1 1 rand(3,4)}, ...
{9 4 rand(3,4)}, {3 3 rand(3,4)}, {1 6 rand(3,4)}
};
% Pull out the 2nd numbers and put them into a vector
secondNumber = cell2mat(cellfun(@(x)x(2),data));
% determine the sorted index of the vector
[~, sortIdx] = sort(secondNumber);
%Resort the data
dataSorted = data(sortIdx);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by