Replacing NaN from doubles in a cell array with blank

"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
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.
Adrian
Adrian 2014 年 10 月 31 日
Sure!

サインインしてコメントする。

回答 (1 件)

Ced
Ced 2014 年 10 月 31 日
編集済み: Ced 2014 年 10 月 31 日

0 投票

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 件のコメント

Adrian
Adrian 2014 年 10 月 31 日
Thank you, Ced. No, I don't have to use cellfun, it just seemed a promising way to solve my NaN-problem. The for-loop works fine but I don't want to replace the NaNs with 0 since I later will use the cell array for polyfitting and don't want MATLAB to calculate and include the "0" for/in the plot. I neither want MATLAB to plot the NaNs that's why I just want to replace the NaNs with a blank double.
There was a question asked before which is similar to my problem now but only dealt with doubles instead of cell arrays: http://www.mathworks.ch/matlabcentral/answers/90172-replace-nan-with-blanks
Is there any solutions/workarounds to my problem?
David Young
David Young 2014 年 10 月 31 日
There's no such thing as a blank double. You have to use NaN to indicate missing data. You may need to unpack individual rows or columns from the numeric matrices, then remove the NaNs, then call polyfit.
Adrian
Adrian 2014 年 11 月 1 日
That's a pity... So, I'm going to rephrase my question: Is there a way for "polyfit" which will ignore the NaNs (something like "nanpolyfit")?
I've eliminated all the NaNs as you've suggested, David. The problem here is that I'm ending up with doubles of different lengths which results in an error message when plotting the polyfitted data ("Vectors must be the same lengths"). Therefore, I have to preserve the length of the doubles (=75).
Ced
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.

サインインしてコメントする。

カテゴリ

製品

質問済み:

2014 年 10 月 31 日

編集済み:

Ced
2014 年 11 月 2 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by