4.5

4.5 | 2 ratings Rate this file 40 downloads (last 30 days) File Size: 2.26 KB File ID: #19730

CELL2FLOAT

by Jos (10584)

 

27 Apr 2008 (Updated 04 Jan 2010)

Code covered by the BSD License  

converts cell array into scalar float array (v4.0, jan 2010)

Download Now | Watch this File

File Information
Description

CELL2FLOAT - converts cell array into scalar float array

M = CELL2FLOAT(C) returns a float array M with the same size as the cell array C. C can be any N-dimensional cell array. Cells of C that contain a scalar float (single or double) are put in corresponding locations of M. Cells that contain other datatypes (e.g., strings, arrays, empty, integers, etc.) yield NaNs in the corresponding locations of M.
 
M = CELL2FLOAT(C,F) uses the value F instead of NaN to fill these latter locations. F should be a scalar float (single or double).

M = CELL2FLOAT(C,F,'error') will cause the program to error if a cell does not contain a scalar float. If F is empty ([]), NaN will be used as the filler value.
 
[M,Q] = CELL2FLOAT(C, ..) returns a logical array Q with logical ones (true) where the values of C are floats, and logical zeros (false) elsewhere.

Examples:
       C = {single(1) 2 'x' [] ; 1:3 complex(1,2) uint8(1) Inf}
       M = cell2float(C)
       % -> [ 1.00 2.00 NaN NaN ;
       % NaN 1.00 + 2.00i NaN Inf ]
 
       cell2float({1 2 [] 3 [] 5},999)
       % -> [ 1 2 999 3 999 5 ]
 
       cell2float({1,2,'x'},[],'error')
       % ??? Error using ==> cell2float at 80
       % Not all cells contain a scalar float.
 
     See also cell2mat, cellfun, num2cell

version 4.0 jan 2010

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
cell2num
This submission has inspired the following:
Pivot/unPivot

MATLAB release MATLAB 6.5 (R13)
Other requirements should work in most ML releases
Zip File Content  
Other Files cell2float.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
22 Sep 2008 A K

Works the magic in a short time. I needed to convert a cell vector which had numerous empty cells and a few sparse 1's into an equivalent vector with 1's and 0's. I had not managed to find a fast, vectorized approach until now. Thanks!

03 Dec 2009 Oleg Komarov

I would prefer the default behaviour to throw an error if a mixed datatype cell array is supplied.
Its auto-NaN behavior can pass unobserved and can lead to late detection of the error if the input was meant to be a cell array of scalars n the first place.
This means that if i believe my Input is a cell array of scalars but it is not i will notice only by supplying the index output of cell2float and calling an any(IDX).

So i'd like a more transparent syntax as:
cell2float(In,NaN)
and
cell2float(In) --> error if In mixed datatypes.

03 Dec 2009 Oleg Komarov

I added on line 49 (shifting the rest down) these lines of code:
if nnz(~cellfun('isclass',C,class(C{1}))) > 0
        error('cell2float:mxdClassInput',...
'Filler not supplied. All contents of C must be of the same data type.')
    end

I'll add your cell2float (modified with those lines) + license to my next submission (instead of using cell2mat) if you agree.

03 Dec 2009 Oleg Komarov  
30 Dec 2009 Oleg Komarov

>> cell2float({23,35},'error')
ans =
#rorerror

line 64 isn't consistent with char preallocation.

I substituted line 57 with:
if nnz(~cellfun('isclass',C,class(C{1}))) > 0
        error('cell2float:NonScalerCells','Not all cells contain a scalar float.');
    else Filler = NaN;
    end

Then I deleted line 70-72.
Oleg

Please login to add a comment or rating.
Updates
29 Apr 2008

spelling errors

12 May 2008

spelling description

04 Dec 2009

incorporated the error suggestion by Oleg

07 Dec 2009

updated description

04 Jan 2010

fixed error noted by Oleg. Note that the syntax has changed.

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 09:59:19
cell Jos (10584) 24 Oct 2008 15:43:46
double Jos (10584) 24 Oct 2008 15:43:46
float Jos (10584) 24 Oct 2008 15:43:46
convert Jos (10584) 24 Oct 2008 15:43:46
change Jos (10584) 24 Oct 2008 15:43:46
values Jos (10584) 24 Oct 2008 15:43:46
array Jos (10584) 24 Oct 2008 15:43:46
cell2 Oleg Komarov 03 Dec 2009 18:49:05
10584x Jos (10584) 19 Dec 2009 09:44:34
 

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