Replacing NaN from doubles in a cell array with blank
1 次查看(过去 30 天)
显示 更早的评论
"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 个评论
Image Analyst
2014-10-31
编辑:Image Analyst
2014-10-31
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.
回答(1 个)
Ced
2014-10-31
编辑:Ced
2014-10-31
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 个评论
Ced
2014-11-2
编辑:Ced
2014-11-2
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!