View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.7 | 3 ratings Rate this file 1 Download (last 30 days) File Size: 1.96 KB File ID: #11192 Version: 1.0



Jos (10584) (view profile)


25 May 2006 (Updated )

group values into cells

| Watch this File

File Information

GROUP2CELL - group values into cells
C = GROUP2CELL(V,G) returns a N-by-1 cell array in which the values V are grouped as row vectors into separate cells according to the values in G. Both G and V should have the same number of elements. N is the number of different values in G. Each cell in C is a row vector.
[C,GR] = GROUP2CELL(V,G) also returns the groups in GR, so that C{i} = V(G==GR(i)).
The grouping variable can be a cell array of strings.

[C, GG] = group2cell([1 2 3 4 5 6],[1 6 2 1 6 1]) ;
% returns the cell array {[1 4 6] ; [3] ; [2 5]} in C and [1 2 6] in GG

years = [1956 1978 1982 1987 2001 2006] ;
temp = {'normal','cold','hot','normal','cold','hot'} ;
C = group2cell(years,temp)
and NONES, COUNTMEMBER (on the Matlab File Exchange)

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
10 Aug 2014 Jos (10584)

Jos (10584) (view profile)

@Qifan something like this should work: cat(1, cell1{:}, cell2{:})

Comment only
28 Jul 2014 Qifan

Qifan (view profile)

Thanks, it is useful, but how can we convert the cell array back to a column?
For example, if cell1 is 1*6 [1 2 3 4 5 6], and cell2 is 1*4 [7 8 9 10], how can we get the column vector [1 2 3 4 5 6 7 8 9 10]'? Many thanks

17 Mar 2011 Rossella  
26 May 2006 Richard Johnson

There are 3 common ways to deal with data in a "ragged" array: pad with nan or 0, use a cell array, or use a grouping variable. This function provides a useful translation from grouping variable organization to cell organization.
It would be improved if the variable names in the help lines more closely matched those in the function line.
A useful but more complex generalization would be to allow a non-numeric grouping variable.

26 May 2006 Duane Hanselman

Based on the example given in the description, I don't understand how "the values V are grouped as row vectors into separate cells according to the values in G"??? I don't see the correspondence at all. Can you provide a more illustrative example and explanation. Also, in what applications might this function be useful? As it stands, this function does not make any sense to me.

Comment only

Contact us