Replace zeros with NaN's - when there are already NaN's in the matrix?

24 次查看(过去 30 天)
Hi
I have a large square matrix which is made up of smaller square matrices which have some NaN values. I used 'blkdiag' to combine the matrices diagonally and all the gaps were filled in with zeros.
I now wish to replace all of the zeros in the large matrix (F) with NaN's.
I tried F(find(~F))=NaN but I get an error saying NaN's cannot be converted to logicals, which makes sense but now I'm not sure how I can get round it. I am fairly new to Matlab.
Any help would be much appreciated.

采纳的回答

Sean de Wolski
Sean de Wolski 2011-6-9
No reason for find and you can't convert nans to logicals.
F(F==0) = nan;
  1 个评论
scour_man
scour_man 2011-6-9
Great, thanks. Seems obvious now! And yes its to stop zero values showing up in surf and contour plots in between areas of data. Thank you

请先登录,再进行评论。

更多回答(2 个)

Ivan van der Kroon
Change to double first:
F=double(F);
F(find(~F))=NaN;
Just curious; why would you need this?

Andrei Bobrov
Andrei Bobrov 2011-6-9
F(~isnan(F))=NaN;

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by