MATLAB Answers

0

Concatenating cell arrays with a different number of columns

Asked by Jillyn Tittle on 13 Jun 2019
Latest activity Commented on by Jillyn Tittle on 13 Jun 2019
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

  0 Comments

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by Guillaume
on 13 Jun 2019
Edited by Guillaume
on 13 Jun 2019
 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{:})

  5 Comments

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!!!!

Sign in to comment.