Find the longest array in an array of vectors

조회 수: 26 (최근 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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by