# How can I make each cell array consistent in length?

2 visualizzazioni (ultimi 30 giorni)
Farshid Daryabor il 2 Mar 2020
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))]
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Alex Mcaulley il 2 Mar 2020
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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Alex Mcaulley il 2 Mar 2020
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 il 2 Mar 2020
Thanks, I really appreciate

Accedi per commentare.

### Categorie

Scopri di più su Data Types in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by