File Exchange Pick of the Week

September 4th, 2009

Concatenate vectors of unequal lengths

Brett's Pick this week is padcat, by Jos--one of the File Exchange's most popular contributors.

MATLAB has a few containers that are useful for storing heterogeneous data. In particular, one can store just about any combination of variables or data types in cell arrays, structures, or (if you have the always useful Statistics Toolbox) dataset arrays. Sometimes you want something lighter-weight, and easy to manipulate. If you just wanted to combine several row or column vectors into a matrix, the function cat makes short work of it, if the vectors are the same length and orientation (all row vectors or all column vectors). If, on the other hand, they had the same orientation but different lengths, cat wouldn't know what to do with them.

With padcat, you could automatically combine them, padding shorter vectors with NaNs as necessary.

Say, for example, that you had four row vectors of different length:

a = 1:5 ; b = 1:3 ; c = [] ; d = 1:4 ;

and you wanted to create from them a single matrix. Try padcat:

M = padcat(a,b,c,d) % all are row vectors
M =
     1     2     3     4     5
     1     2     3   NaN   NaN
   NaN   NaN   NaN   NaN   NaN
     1     2     3     4   NaN

As an added bonus, Jos's function can generate a second output that contains a binary mask showing true (or 1) where elements of M originated from an input vector, and false (or 0) where they were padded. This can be useful if any of the component vectors themselves contain NaNs. For instance:

a = [1:3]' ; b = [] ; c = [1;NaN] ;
[M,tf] = padcat(a,b,c) % all are column vectors
M =
     1   NaN     1
     2   NaN   NaN
     3   NaN   NaN
tf =
     1     0     1
     1     0     1
     1     0     0

(Note that the second element in the third column of tf [i.e., row 2, column 3] is 1, indicating that the NaN in that position of M originated in vector c.)

Just a nice utility function to have around. Thanks, Jos!

Comments?


Get the MATLAB code

Published with MATLAB® 7.8

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.