change only those zeros to NaN if all in row are 0
5 次查看(过去 30 天)
显示 更早的评论
I have
a =
1 2 3
0 0 0
2 1 0
4 5 0
0 0 0
2 0 1
I need
b =
1 2 3
NaN NaN NaN
2 1 0
4 5 0
NaN NaN NaN
2 0 1
i.e. only if all elements in row are 0 replace with NaN
0 个评论
采纳的回答
Thomas
2012-6-6
a(any(a,2)==0,:)=NaN;
b=a
3 个评论
Image Analyst
2012-6-6
It works because any() looks for non-zeros, so it's only zero if there are no non-zeros, in other words, if they are all zeros. Perhaps my answer (not my comment above) might be more intuitive though - it used all().
更多回答(1 个)
Image Analyst
2012-6-6
% Make a copy so we don't change a.
b=a
% Find out which rows are all zeros.
nonZeroRows = all(b == 0, 2)
% Assign all columns of the all-zero rows to nans.
b(nonZeroRows, :) = NaN
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!