change only those zeros to NaN if all in row are 0
Show older comments
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
Accepted Answer
More Answers (1)
Image Analyst
on 6 Jun 2012
% 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
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!