Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cell2mat and empty values cell

803 views
Skip to first unread message

b mei

unread,
May 27, 2008, 5:57:02 AM5/27/08
to
I am not being able to use cell2mat in a matrix containing
empty values, because the empty cells from excel are saved
at matlab as int 16, and the rest of the matrix that
contain numbers are saved as double.

So when I use cell2mat with empty values and filled cells I
receive an error message for different data types.

How can I use cell2mat with empty cells and filled cells?

The cells are not always going to be empty, and the program
is working fine with all the cells filled.

But once I have to use some cells empty it doesn t work any
more.

us

unread,
May 27, 2008, 6:19:02 AM5/27/08
to
"b mei":
<SNIP does not want to feel empty...

> How can I use cell2mat with empty cells and filled

cells...

one of the solutions

c={
1 2 []
[] 3 4
};
ix=cellfun(@isempty,c);
c(ix)={nan}; % -or- any other marker...
disp(c);

us

Daniel Goodman

unread,
Jun 29, 2009, 9:14:02 AM6/29/09
to
> > How can I use cell2mat with empty cells and filled
> cells...
>
> one of the solutions
>
> c={
> 1 2 []
> [] 3 4
> };
> ix=cellfun(@isempty,c);
> c(ix)={nan}; % -or- any other marker...
> disp(c);
>
> us

The above takes AGES if the cell matrix is large. I have a 33845x16 matrix of all numbers (except for some blank cells) and finding the empty cells takes 500 seconds (!). There must be a faster way (aside from not using cell matrices)?

Daniel Goodman

unread,
Jun 29, 2009, 9:30:17 AM6/29/09
to
"Daniel Goodman" <dbgo...@gmail.com> wrote in message <h2aemq$1d$1...@fred.mathworks.com>...

> The above takes AGES if the cell matrix is large. I have a 33845x16 matrix of all numbers (except for some blank cells) and finding the empty cells takes 500 seconds (!). There must be a faster way (aside from not using cell matrices)?

Another note: I solved my problem by getting around this whole 'finding empty cells' businesss. I was trying to write mixed format data (numbers and MySQL nulls, '\N') by finding all the empty cells and replacing them with NaN then regexping them out with perl afterwards.

Instead of trying to convert the cell data to a numeric matrix in the first place, I found my solution here:
http://www.mathworks.de/matlabcentral/newsreader/view_thread/243930#656112

It discusses how to speed up writing cell arrays directly.

I can write cells directly now without worrying about the file output speed (before it was taking 3-5 minutes for a 33845 x 16 array!).

Andrew

unread,
Jun 29, 2009, 9:33:01 AM6/29/09
to
"Daniel Goodman" <dbgo...@gmail.com> wrote in message <h2aemq$1d$1...@fred.mathworks.com>...


Try this instead:

ix=cellfun('isempty', c);

HTH

Andrew

us

unread,
Jun 29, 2009, 9:35:02 AM6/29/09
to
"Daniel Goodman" <dbgo...@gmail.com> wrote in message <h2aemq$1d$1...@fred.mathworks.com>...

well, looking back at this old reply from 27 May, 2008 10:19:02...

- firstly, 33845 * 16 (=541520) is a somewhat biggish cell...
and you spend a merely ~0.00092333 s/cellmember
- time/profile this solution with a for-loop...

us

Bruno Luong

unread,
Jun 29, 2009, 10:53:01 AM6/29/09
to
You are lucky, cellfun with isempty can be fast, providing correct calling syntax
http://www.mathworks.com/matlabcentral/newsreader/view_thread/25381

c=num2cell(rand(33845,16));
s=sprand(size(c,1),size(c,2),1e-2);
c(find(s))={[]};

tic; ix=cellfun('isempty', c); toc % Elapsed time is 0.014942 seconds.

tic; ix=cellfun(@isempty, c); toc % Elapsed time is 0.622717 seconds.

% Bruno

Jos

unread,
Jun 29, 2009, 10:57:00 AM6/29/09
to
"Daniel Goodman" <dbgo...@gmail.com> wrote in message <h2aemq$1d$1...@fred.mathworks.com>...

Ages?? Something must went wrong. Anyways, you might be interested in my cell2float function:
http://www.mathworks.com/matlabcentral/fileexchange/19730

A = num2cell(rand(33845,16)) ;
idx = ceil(numel(A) * rand(10,1)) ; A(idx) = {[]} ;
tic ; B = cell2float(A) ; toc ;
% Elapsed time is 0.632537 seconds.
isequal(find(isnan(B)),sort(idx))
% ans = 1

hth
Jos

Dimitris Kokkinos

unread,
May 7, 2014, 4:34:09 PM5/7/14
to
> A = num2cell(rand(33845,16)) ;
> idx = ceil(numel(A) * rand(10,1)) ; A(idx) = {[]} ;
> tic ; B = cell2float(A) ; toc ;
> % Elapsed time is 0.632537 seconds.
> isequal(find(isnan(B)),sort(idx))
> % ans = 1

I copied your suggestion at matlab but an error occured:
<<??? Undefined function or method 'cell2float' for input arguments of
type 'cell'.>>

Can you help me and tell me what went wrong please?I try to understand
the function cell2float.Thnx in advance.

dpb

unread,
May 7, 2014, 5:08:17 PM5/7/14
to
On 5/7/2014 3:34 PM, Dimitris Kokkinos wrote:
...

> I copied your suggestion at matlab but an error occured:
> <<??? Undefined function or method 'cell2float' for input arguments of
> type 'cell'.>>
>
> Can you help me and tell me what went wrong please?I try to understand
> the function cell2float.Thnx in advance.

<http://www.mathworks.com/matlabcentral/fileexchange/19730-cell2float>

--
0 new messages