4.0

4.0 | 1 rating Rate this file 14 downloads (last 30 days) File Size: 1.96 KB File ID: #11192

GROUP2CELL

by Jos (10584)

 

25 May 2006 (Updated 09 Aug 2006)

Code covered by the BSD License  

group values into cells

Download Now | Watch this File

File Information
Description

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)

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
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.

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.

Please login to add a comment or rating.
Updates
09 Aug 2006

added functionality of grouping variable to be a cell array of strings

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 08:26:52
cell Cristina McIntire 23 Feb 2009 15:31:51
group Cristina McIntire 23 Feb 2009 15:31:51
count Cristina McIntire 23 Feb 2009 15:31:51
cellfun Cristina McIntire 23 Feb 2009 15:31:51
hist Cristina McIntire 23 Feb 2009 15:31:51
cellfun Weimin 11 Nov 2009 00:51:27
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com