Finding largest data from table

3 ビュー (過去 30 日間)
Sebastian Daneli
Sebastian Daneli 2021 年 11 月 15 日
編集済み: Seth Furman 2021 年 11 月 16 日
Lets say that I have a table containing three matricies of different lengths.
X1=[9 6 9 0;3 2 7 0];
X2=[0 2;4 0];
X3=[3 1 2; 8 9 7];
X=table(X1,X2,X3)
X = 2×3 table
X1 X2 X3 ________________ ______ ___________ 9 6 9 0 0 2 3 1 2 3 2 7 0 4 0 8 9 7
How can i I find the longest one? Is there a way to find more then one matrix of a specific lenght? Lets say the ones that are the longest.
  1 件のコメント
Chunru
Chunru 2021 年 11 月 15 日
Show what the data you have by giving a minimum example.

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

採用された回答

Chunru
Chunru 2021 年 11 月 15 日
編集済み: Chunru 2021 年 11 月 15 日
X1=[9 6 9 0;3 2 7 0];
X2=[0 2;4 0];
X3=[3 1 2; 8 9 7];
X4 = X1;
X=table(X1,X2,X3,X4)
X = 2×4 table
X1 X2 X3 X4 ________________ ______ ___________ ________________ 9 6 9 0 0 2 3 1 2 9 6 9 0 3 2 7 0 4 0 8 9 7 3 2 7 0
len = table2array(varfun(@(x) size(x, 2), X));
% max will find 1 entry only
[lmax, idx] = max(len)
lmax = 4
idx = 1
% if you want multiple entries
idxall = find(len == lmax)
idxall = 1×2
1 4
  2 件のコメント
Sebastian Daneli
Sebastian Daneli 2021 年 11 月 15 日
Thx
Seth Furman
Seth Furman 2021 年 11 月 16 日
編集済み: Seth Furman 2021 年 11 月 16 日
The only thing I would add is that
@(x) size(x, 2)
ans = function_handle with value:
@(x)size(x,2)
can be replaced with
@width
ans = function_handle with value:
@width
since the width function returns the number of variables in a table or the number of columns in an array.
e.g.
X1 = [9 6 9 0;3 2 7 0];
X2 = [0 2;4 0];
X3 = [3 1 2; 8 9 7];
X4 = X1;
X = table(X1,X2,X3,X4)
X = 2×4 table
X1 X2 X3 X4 ________________ ______ ___________ ________________ 9 6 9 0 0 2 3 1 2 9 6 9 0 3 2 7 0 4 0 8 9 7 3 2 7 0
varfun(@width, X, "OutputFormat", "uniform")
ans = 1×4
4 2 3 4

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by