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.
> 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)?
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!).
Try this instead:
ix=cellfun('isempty', c);
HTH
Andrew
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
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
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