How can I make each cell array consistent in length?

조회 수: 4 (최근 30일)
Farshid Daryabor
Farshid Daryabor 2020년 3월 2일
댓글: Farshid Daryabor 2020년 3월 2일
I'm really grateful for anyone telling me how to make cell arrays equal in length (please find attached). The following code doesn'y work.
N = cellfun(@numel, T_mon);
>> M = min(N);
>> newN = M * ceil(N / M);
>> padfun = @(k) [T_mon{k} zeros(1, newN(k) - N(k))] ;
>> T_mon_new = arrayfun(padfun, 1:numel(T_mon) , 'un', 0) ;
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in @(k)[T_mon{k},zeros(1,newN(k)-N(k))]

채택된 답변

Alex Mcaulley
Alex Mcaulley 2020년 3월 2일
Do you mean this?
N = cellfun(@numel, T_mon);
M = max(N);
T_mon_new = cellfun(@(a) [a; zeros(M - numel(a),1)],T_mon,'uni',0);
  댓글 수: 3
Alex Mcaulley
Alex Mcaulley 2020년 3월 2일
Yes, just changing:
N = cellfun(@numel, T_mon);
M = max(N);
T_mon_new = cellfun(@(a) [a; nan(M - numel(a),1)],T_mon,'uni',0);
Farshid Daryabor
Farshid Daryabor 2020년 3월 2일
Thanks, I really appreciate

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by