File Exchange

version 1.4.0.0 (3.21 KB) by
concatenate vectors with different lengths by padding with NaN

Updated 05 Dec 2018

Editor's Note: This file was selected as MATLAB Central Pick of the Week

M = PADCAT(V1, V2, V3, ..., VN) concatenates the vectors V1 through VN
into one large matrix. All vectors should have the same orientation,
that is, they are all row or column vectors. The vectors do not need to
have the same lengths, and shorter vectors are padded with NaNs.
The size of M is determined by the length of the longest vector. For
row vectors, M will be a N-by-MaxL matrix and for column vectors, M
will be a MaxL-by-N matrix, where MaxL is the length of the longest
vector.

Examples:
a = 1:5 ; b = 1:3 ; c = [] ; d = 1:4 ;
% -> 1 2 3 4 5
% 1 2 3 NaN NaN
% NaN NaN NaN NaN NaN
% 1 2 3 4 NaN
CC = {d.' a.' c.' b.' d.'} ;
% 1 1 NaN 1 1
% 2 2 NaN 2 2
% 3 3 NaN 3 3
% 4 4 NaN NaN 4
% NaN 5 NaN NaN NaN

[M, TF] = PADCAT(..) will also return a logical matrix TF with the same
size as R having true values for those positions that originate from an
input vector. This may be useful if any of the vectors contain NaNs.

Example:
a = 1:3 ; b = [] ; c = [1 NaN] ;
% find the original NaN
[Vev,Pos] = find(tf & isnan(M))
% -> Vec = 3 , Pos = 2
This second output can also be used to change the padding value into something else than NaN.
M(~tf) = 99 % change the padding value into 99

Scalars will be concatenated into a single column vector.

nones, group2cell (Matlab File Exchange)

### Cite As

david gasperini

Perfect

Jos (10584)

Thanks Michael.
Can you email me a small example of your desired functionality?

Michail Kanoupakis

Thank you so much Jos. Great job!

Is there any way to apply it in a cell matrix with elements of different sizes or should I put as an input of the padcat function each row of the cell matrix ?

Lucas Galdino

Thank you for the amazing work!

sinan salim

how can us padcat for two dimsions array?

Hassan

can the function work for cell arrays of strings?

Abel Castro

Excelent function. Thank you Jos!

Jos (10584)

@beginner94 The description says "most releases", not "every release". Perhaps it is time for you to upgrade? Good old steam trains are also no longer running in most (sic!) countries :-)

beginner94

Unfortunately it isn't really compatible with every release. I am still using R2007b and "narginchk" for example doesn't exist yet.

Aijing Feng

very useful

Emilio Llorente-Cano

Very very useful. Thank you.

Symone Alexander

Jonas Allgeier

Super useful piece of code, especially for creating many patches with a single call.

Wonderful code. Works very well.

Excellent ... Thanks a lot

free5721

free5721

Excellent! Thank you!

Michel Nieuwoudt

works brilliantly, thank you!

Vagner

Thanks!

Anne Seifert

Mubashir

Johann Thurn

This is really nice. However, if you get the vectors via the extractfield function from a struct and have an empty vector, there may occur a problem, if you try to concatenate this with a 1x1 matrix. The empty vector can either be 1x0 or 0x1, one of which will be the wrong size to work with.

midhun

@Jos Thanks a lot. Can you implement the same code for Cell (strings) array? When I pass a set of strings array I get the error

Conversion to double from cell is not possible.
M(M==0) = X ; % put the values in

midhun

@Jos Thanks a lot. Can you implement the same code for strings array? When I pass a set of strings array I get the error

Conversion to double from cell is not possible.
M(M==0) = X ; % put the values in

Gregory Brittingham

Dimitri

Great. Really useful for data sets with different number of points, no loop needed anymore! Thanks

Kevin Quinn

File works perfectly! I second Joana's comments.

Greg

Harun Cetinkaya

Really thank you very much... It is really what i need...

Joana

That was EXACTLY what I was looking for!
Many thanks

Jonathan Sullivan

@German. You may want to check out the function catpad, it works on marticies and multidimensional arrrays.
http://www.mathworks.com/matlabcentral/fileexchange/33453

Germán

Germán

Nice utility, can it be modified to work with matrices and multidimensional arrays?

Laurence Johns

Kent Conover

Works as advertised - many thanks!

##### MATLAB Release Compatibility
Created with R2018a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux