# Thread Subject: Concatenate vectors of different lengths (auto-pad?)

 Subject: Concatenate vectors of different lengths (auto-pad?) From: Alan_Weiss Date: 13 Jun, 2012 18:03:46 Message: 1 of 4 I wonder if anyone has a nice way of automatically padding vectors (or matrices) with 0 so they can be concatenated. For example, suppose I have vectors A = [1 2 3] B = [3 0 4 0 5] C = [2 1] I would like to have a function supervertcat(A,B,C) that outputs the matrix [1 2 3 0 0 3 0 4 0 5 2 1 0 0 0] I would also like this to work for matrices: A = [1,2;...      3,4] B = [1,2,3] C = supervertcat(A,B) = [1,2,0 3,4,0 1,2,3] Does anyone have a nice way of doing this? I have seen the file exchange submissions PADADD, PADCAT, and CATPAD, which seem fine, but are a bit more heavyweight than I am seeking. I suspect there is a cute 2- or 3-liner out there somewhere. Alan Weiss MATLAB mathematical toolbox documentation
 Subject: Concatenate vectors of different lengths (auto-pad?) From: Bruno Luong Date: 13 Jun, 2012 19:06:07 Message: 2 of 4 Alan_Weiss wrote in message ... > I wonder if anyone has a nice way of automatically padding vectors (or > matrices) with 0 so they can be concatenated. > > For example, suppose I have vectors > A = [1 2 3] > B = [3 0 4 0 5] > C = [2 1] > > I would like to have a function supervertcat(A,B,C) that outputs the matrix > [1 2 3 0 0 > 3 0 4 0 5 > 2 1 0 0 0] > > I would also like this to work for matrices: > A = [1,2;... > 3,4] > B = [1,2,3] > C = supervertcat(A,B) = > [1,2,0 > 3,4,0 > 1,2,3] > > Does anyone have a nice way of doing this? I have seen the file exchange > submissions PADADD, PADCAT, and CATPAD, which seem fine, but are a bit > more heavyweight than I am seeking. I suspect there is a cute 2- or > 3-liner out there somewhere. > 4 lines, and not very cute, but anyway here is my proposal: function C = supervertcat(varargin) % C = supervertcat(A1, A2, ...) [i j v] = cellfun(@find, varargin, 'uni',0); r = [0 cumsum(cellfun('size',varargin,1))]; [i j v] = arrayfun(@(k) deal(r(k)+i{k}(:),j{k}(:),v{k}(:)), 1:nargin,'uni',0); C = accumarray([cat(1,i{:}) cat(1,j{:})], cat(1,v{:})); end
 Subject: Concatenate vectors of different lengths (auto-pad?) From: Bruno Luong Date: 13 Jun, 2012 19:44:07 Message: 3 of 4 If it was my code, I probably do something like this: function C = supervertcat(varargin) % C = supervertcat(A1, A2, ...) ncol = cellfun('size',varargin,2); maxcol = max(ncol); for k = 1:nargin     if ncol(k) < maxcol         varargin{k}(end,maxcol) = 0;     end end C = cat(1, varargin{:}); end
 Subject: Concatenate vectors of different lengths (auto-pad?) From: Alan_Weiss Date: 14 Jun, 2012 13:47:43 Message: 4 of 4 On 6/13/2012 3:44 PM, Bruno Luong wrote: > If it was my code, I probably do something like this: > > function C = supervertcat(varargin) > % C = supervertcat(A1, A2, ...) > > ncol = cellfun('size',varargin,2); > maxcol = max(ncol); > for k = 1:nargin > if ncol(k) < maxcol > varargin{k}(end,maxcol) = 0; > end > end > C = cat(1, varargin{:}); > > end Thanks, Bruno, this is pretty clear. Alan Weiss MATLAB mathematical toolbox documentation

## Tags for this Thread

### Add a New Tag:

Separated by commas
Ex.: root locus, bode

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Feed for this Thread