Convert cell array to matrix with two columns and sort rows by first column value

1 回表示 (過去 30 日間)
Hello,
I have a large Cell array with multiple cells. Every these cells has n-rows and two columns.
I want to convert this cell-array to one large matrix with j x 2 double matrix (j is sum value of all rows of all cells)
Cell_x =
1×8 cell array
Columns 1 through 4
{13×2 double} {10×2 double} {9×2 double} {11×2 double}
Columns 5 through 8
{9×2 double} {12×2 double} {6×2 double} {7×2 double}
Matrix_x =
77 x 2 double
after it, I want to sort the rows by values of the first column:
Test_unsorted =
4 45
8 78
9 77
1 68
Test_sorted =
1 68
4 45
8 78
9 77
Thank you for your help!

採用された回答

Stephen23
Stephen23 2021 年 2 月 10 日
編集済み: Stephen23 2021 年 2 月 10 日
More robust and more efficient than using cell2mat:
M = vertcat(Cell_x{:});
M = sortrows(M,1)
  1 件のコメント
Nik Rocky
Nik Rocky 2021 年 2 月 10 日
Thank you, I get the same answer by myself and was writing a solution here =)

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

その他の回答 (1 件)

the cyclist
the cyclist 2021 年 2 月 10 日
編集済み: the cyclist 2021 年 2 月 10 日
I think this does what you want:
% Set RNG seed, for repeatability
rng default
% Make up some data
Cell_x = {rand(13,2),rand(7,2)};
% Convert cell to matrix, and sort rows by first column
sortrows(cell2mat(Cell_x'),1)
ans = 20×2
0.0318 0.9502 0.0975 0.7922 0.1270 0.1419 0.1576 0.9340 0.1712 0.6948 0.2769 0.0344 0.2785 0.9595 0.3922 0.0971 0.5469 0.6557 0.6324 0.9157

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by