Code covered by the BSD License  

Highlights from

5.0 | 1 rating Rate this file 11 Downloads (last 30 days) File Size: 6.93 KB File ID: #28916
image thumbnail



Jan Simon (view profile)


Concatenate cell elements to a vector - fast C-Mex

| Watch this File

File Information

CELL2VEC - Concatenate cell elements to a vector
The elements of arrays, which are elements of the input cell, are concatenated to a vector.
V = Cell2Vec(C)
  C: Cell array of any size. Accepted classes: all numerical types
     (DOUBLE, SINGLE, (U)INT8/16/32/64), LOGICAL, CHAR.
     All non-empty cell elements must be the same class.
  V: [1 x N] vector of all elements. The class of V is the class of the
     cell elements of C.

  C = {1, 2; 3, 4};
  V = Cell2Vec(C) % [1, 3, 2, 4] columnwise order
  M = reshape(V, size(C)) % [1, 2; 3, 4]

NOTE: If the cell elements are vectors, this equals CAT, but is remarkably
faster for large cells: It seems that CAT fails for a smart pre-allocation?!
E.g. for a {1 x 5000} cell string with a total of 200.000 CHARs:
  CAT (Matlab 6.5) => 60 sec
  CAT (Matlab 7.8) => 5 sec
  Cell2Vec: => 1.2 sec (1.5GHz Pentium-M).

Pre-compiled Mex-files:

Run the unit-test uTest_Cell2Vec after compiling.

Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
        Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008
Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit

See also: CStr2String, CELL2MAT.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
12 Feb 2012 Jan Simon

Jan Simon (view profile)

@Misha, you have to compile the C-file as described in the section "COMPILATION" in the help text - see "help Cell2Vec".

Comment only
12 Feb 2012 Misha

Misha (view profile)

Hi Jan,
Am I doing something wrong in trying to use this patch? I've dropped it in my main matlab folder. When I try to run:

a =

1 5 9
2 6 10
3 7 11
4 8 12

>> v = cell2vec(a)

i get an error:
Error using Cell2Vec (line 41)
Cannot find Mex file of Cell2Vec

Comment only
01 Oct 2010 Quant Guy

Quant Guy (view profile)

This is very useful tool and important submission. I was able trivially to reimplement the out of the box implementation of cell2mat which is very slow and got huge increases in overall speed!!

Contact us