Concatenating cell arrays with a different number of columns

I have a 64x1 cell array that looks like this, I would like to vertically concatenate the cells in this array, and pad the rows with less columns with zeros blah.PNG

 Accepted Answer

maxcols = max(cellfun('size', NonC_tRNAs, 2)); %get the number of columns of the widest array
padded = cellfun(@(m) [m, zeros(size(m, 1), maxcols - size(m, 2))], NonC_tRNAs, 'UniformOutput', false); %pad each array
NonC_tRNAs_Reference = vertcat(padded{:})

7 Comments

This results in an error in the first line:
error using cellfun
cellfun only works on cells
Read the answer once again. The way Guillaume showed yourcellarray is the name of your cell array. Since you beautifully cropped the screenshot, hence thy name was shown for an example.
Apologies, I just did a screengrab because inputting all of the manipulations to get to this output would be cumbersome.
I did in fact put my cell array in for yourcellarray
maxcols = max(cellfun('size',2,NonC_tRNAs))
padded = cellfun(@(m) [m, zeros(size(m,1),maxcols - size(m,2))], NonC_tRNAs, 'UniformOutput', false)
NonC_tRNAs_Reference = vertcat(padded{:})
Capture.PNG
I had the inputs to cellfun('size', ...) in the wrong order. Fixed now.
The correction worked wonderfully! Thank you!!!!
Sorry if this is a dumb question, but what is "m" in this instance?
padded = cellfun(@(m) [m, zeros(size(m, 1), maxcols - size(m, 2))], NonC_tRNAs, 'UniformOutput', false); %pad each array
" what is "m" in this instance?"
An arbitrary name for the input variable of the anonymous function called by cellfun. It will be in turn each element of the same array.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019a

Asked:

on 13 Jun 2019

Commented:

on 16 Mar 2020

Community Treasure Hunt

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

Start Hunting!