フィルターのクリア

How do you extract the elements of a cell that have the largest length?

3 ビュー (過去 30 日間)
L'O.G.
L'O.G. 2022 年 3 月 16 日
回答済み: David Hill 2022 年 3 月 16 日
Given a cell A, I can find the maximum length by
[s,d] = cellfun(@size,A);
out = max([s,d]);
But how do I find the elements in A that have the length given by out? For example, given the cell array consisting of {[7 8 9]} {[6 10]} {[4 5 6 7]} {[1 2 3 4]}, extract {[4 5 6 7]} and {[1 2 3 4]}, which each have a length of 4.

採用された回答

Mathieu NOE
Mathieu NOE 2022 年 3 月 16 日
hello
here my suggestion
A = [{[7 8 9]} {[6 10]} {[4 5 6 7]'} {[1 2 3 4]'}];
[s,d] = cellfun(@size,A);
out = max([s,d],[],'all');
ind = find(d == out | s == out) ;
A_selected = A(ind)

その他の回答 (2 件)

Stephen23
Stephen23 2022 年 3 月 16 日
C = {[7,8,9],[6,10],[4,5,6,7],[1,2,3,4]}
C = 1×4 cell array
{[7 8 9]} {[6 10]} {[4 5 6 7]} {[1 2 3 4]}
N = cellfun('length',C);
X = max(N)==N;
D = C(X)
D = 1×2 cell array
{[4 5 6 7]} {[1 2 3 4]}

David Hill
David Hill 2022 年 3 月 16 日
[s,d] = cellfun(@size,A);
m = max([s,d],[],'all');
idx=s==m|d==m;
Anew=A(idx);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by