In a matrix how to replace zero by NaN.?

899 views (last 30 days)
Safi ullah
Safi ullah on 19 Apr 2018
Commented: 拓郎 下山 on 3 May 2021
Hi every one, I have a matrix A=1×180. All values of matrix A are either zeros or negative numbers. I need to replace all the zeros by NaN, noted that zeros are also randomly distributed in matrix A. I have tried for loop but do not get the desired result. Any guidance will be appreciated thanks.
  1 Comment
拓郎 下山
拓郎 下山 on 3 May 2021
I have the same problem.
I change 0 value to NaN value in nifti file of int16.
I try "V(V==0) = NaN;"
But, it doesn't work. Could you give me the answer?
Thank you for reading.

Sign in to comment.

Accepted Answer

njj1
njj1 on 19 Apr 2018

Try this:

A(A==0) = NaN;
  4 Comments
Steven Lord
Steven Lord on 21 Apr 2021
If you want to keep the original array around, make a copy of it then replace the values in that copy.
A = randperm(10)
A = 1×10
9 5 7 3 8 10 2 1 4 6
B = A;
B(B > 7) = NaN
B = 1×10
NaN 5 7 3 NaN NaN 2 1 4 6
[A; B] % Use both the original vector (A) and the modified one (B)
ans = 2×10
9 5 7 3 8 10 2 1 4 6 NaN 5 7 3 NaN NaN 2 1 4 6

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!