Code covered by the BSD License

4.77778
4.8 | 10 ratings Rate this file 75 Downloads (last 30 days) File Size: 5.66 KB File ID: #22909 Version: 1.3

### Jos (10584) (view profile)

06 Feb 2009 (Updated )

concatenate vectors with different lengths by padding with NaN

### Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

File Information
Description

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)

Acknowledgements

MATLAB release MATLAB 8.5 (R2015a)
MATLAB Search Path
`/`
Other requirements Should work on most ML releases
14 Apr 2016 Gregory Brittingham

### Gregory Brittingham (view profile)

05 Jun 2015 Dimitri

### Dimitri (view profile)

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

27 Apr 2015 Kevin Quinn

### Kevin Quinn (view profile)

File works perfectly! I second Joana's comments.

26 Sep 2014 Greg

### Greg (view profile)

20 Mar 2013 Harun Cetinkaya

### Harun Cetinkaya (view profile)

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

09 May 2012 Joana

### Joana (view profile)

That was EXACTLY what I was looking for!
Many thanks

26 Oct 2011 Jonathan Sullivan

### Jonathan Sullivan (view profile)

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

Comment only
10 Aug 2011 Germán

### Germán (view profile)

10 Aug 2011 Germán

### Germán (view profile)

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

01 Jul 2009 Laurence Johns

### Laurence Johns (view profile)

04 Jun 2009 Kent Conover

### Kent Conover (view profile)

Works as advertised - many thanks!

07 Oct 2011 1.1

added help on how to change the padding value using the secondary output

24 Feb 2016 1.3

updated contact info, and update argument checking

25 Feb 2016 1.3

updated description