Find the longest array in an array of vectors

8 ビュー (過去 30 日間)
skrowten_hermit
skrowten_hermit 2020 年 10 月 9 日
コメント済み: Ameer Hamza 2020 年 10 月 9 日
I have 5 vectors y1, y2, y3, y4, y5 of variable number of samples or values. I have created a cell array as:
vectorarray = {y1, y2, y3, y4, y5}
I know the classic way of finding the largest number of samples in the elements of the cell array can be done as follows:
maxsamples = 0
for k = 1:numel(vectorarray)
currveclength = length(vectorarray{k})
if currveclength > maxsamples
maxsamples = currveclength
end
end
Is there a simpler way to do it in a single line in Matlab?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 9 日
編集済み: Ameer Hamza 2020 年 10 月 9 日
[max_val, idx] = max(cellfun(@numel, vectorarray))
  2 件のコメント
skrowten_hermit
skrowten_hermit 2020 年 10 月 9 日
Great. Exactly what I wanted! Thanks Ameer.
Ameer Hamza
Ameer Hamza 2020 年 10 月 9 日
I am glad to be of help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by