Replacing NaN from doubles in a cell array with blank
6 views (last 30 days)
Show older comments
"Result" consists of a cell array (19x2) with doubles (75x10). I'd like to replace all the NaN in the the columns (:,2) of all cells (Result{1:25,1:2}) with blanks using cellfun.
Result(cellfun(@(x) any(isnan(x(:,2))),Result)) = {''}
is what I've tried but it blanks me the whole cell and not the double.
Thank you in advance for any help!
2 Comments
Image Analyst
on 31 Oct 2014
Edited: Image Analyst
on 31 Oct 2014
Can you make it easy for us to help you ? Can you attach a mat file with your Result cell array inside it? It would make it easier for people to try things.
Answers (1)
Ced
on 31 Oct 2014
Edited: Ced
on 31 Oct 2014
What exactly do you mean by "replacing with blanks"? Do you want to delete the whole row? or replace by 0? Your matrix consists of doubles, so you can't simply delete certain elements, otherwise the different columns of your matrix would have different lengths.
Do you have to use cellfun? Otherwise, simply do
[ nrows, ncols ] = size(Result);
for j = 1:ncols
for i = 1:nrows
Result{i,j}(isnan(Result{i,j}(:,2)),2) = 0;
end
end
I don't know how you can directly perform assignments in cellfun, but if you absolutely want to use it, you could do:
function x = set_zero_if_nan(x,col)
x(isnan(x(:,col)),col) = 0;
end
and then
Result = cellfun( @(x) set_zero_if_nan(x,2), Result, 'UniformOutput',0 );
4 Comments
Ced
on 2 Nov 2014
Edited: Ced
on 2 Nov 2014
What input are you giving to polyfit? x(:,2) of all cells?
Polyfit doesn't really fit a matrix, it simply uses all points to fit. Meaning, instead of trying to have blanks in your matrix X = [ x1 x2 x3 ... ] which is impossible, simply pass
X = X(:); % save all as one long vector
X = X(~isnan(X)); % eliminate all nan
to polyfit, where X was the matrix you were trying to pass earlier.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!