File Exchange

image thumbnail

GROUP2CELL

version 1.0 (1.96 KB) by

group values into cells

4.66667
3 Ratings

3 Downloads

Updated

View License

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.

Examples:
[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)

See also MAT2CELL, CELLFUN, CAT
and NONES, COUNTMEMBER (on the Matlab File Exchange)

Comments and Ratings (5)

Jos (10584)

Jos (10584) (view profile)

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

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

Rossella

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.

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.

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video