MATLAB Remove NaN From Vector

This tutorial will discuss removing NaN values from a vector using MATLAB’s isnan() function.

MATLAB Remove NaN Values From Vector

The NaN values are undefined values that are generated in the case of arithmetic operations. To remove NaN values from a vector, we have to find their position or index, and then we can set the NaN value equal to null.

Solve for X by Graphing | TI-83 Plu...
Solve for X by Graphing | TI-83 Plus and TI-84 Plus graphing calculators

We can use the isnan() function, which returns a logical array of zeros and ones to find the position of NaN values. The ones in the logical array represent NaN values, and the zeros in the array represent other values.

Suppose the input vector contains complex values; the isnan() function returns 0 when both the real and imaginary parts do not contain NaN and 1 when real or imaginary parts contain Nan. We can use the output of the isnan() function to assign the NaN values a null or empty vector.

For example, let’s create a vector containing NaN values and remove them using the isnan() function. See the code below.

clc
clear

v = [1 2 nan 2 nan]
i = isnan(v)
v(i) = []

Output:

v =

     1     2   NaN     2   NaN


i =

  1×5 logical array

   0   0   1   0   1


v =

     1     2     2

In the above code, the variable i is a logical array that contains ones at the position of NaN values, and we used these index values to set the NaN values equal to the empty vector. We can see in the above output that the NaN values are removed from the vector v.

In the above code, we set the NaN values equal to an empty vector, but we can also replace them with other values like 0. For example, let’s replace the NaN values in a vector with zero.

See the code below.

clc
clear

v = [1 2 nan 2 nan]
i = isnan(v)
v(i) = 0

Output:

v =

     1     2   NaN     2   NaN


i =

  1×5 logical array

   0   0   1   0   1


v =

     1     2     0     2     0

Consider that we have two vectors that depend on each other, like the first value of the first vector depends on the first value of the second vector. There are NaN values in the vectors, and we want to remove them and the corresponding value of the other because the two vectors depend on each other.

We also want the output vectors to have the same size.

We have to remove the NaN values of the first vector and the corresponding values of the second vector even if they are not NaN values, and then we have to do the same thing with the second vector. There can be another method in which we can find the index of NaN values present in both vectors, and then we can take their logical OR to combine the two values, and then we can use the combined result to remove or replace the NaN values.

For example, let’s create two vectors and remove their NaN value and the corresponding values present in both vectors. See the code below.

clc
clear

v1 = [1 2 nan 2 nan]
v2 = [NaN 1 0 2 5]
i1 = isnan(v1);
i2 = isnan(v2);
i = i1 | i2
v1(i) = []
v2(i) = []

Output:

v1 =

     1     2   NaN     2   NaN


v2 =

   NaN     1     0     2     5


i =

  1×5 logical array

   1   0   1   0   1


v1 =

     2     2


v2 =

     1     2

In the above code, we used the symbol | for logical OR to combine the two logical arrays. We can see in the above output that the output vectors do not have NaN values, and their size is equal.

If we want to remove NaN values only if they are present in both vectors at the same position, we can use the logical AND in the place of the logical OR. For example, let’s create two vectors and remove the NaN value from them only if the values are present at the same index.

See the code below.

clc
clear

v1 = [1 2 nan 2 nan];
v2 = [1 0 nan 5 6];
i1 = isnan(v1);
i2 = isnan(v2);
i = i1 & i2;
v1(i) = []
v2(i) = []

Output:

v1 =

     1     2     2   NaN


v2 =

     1     0     5     6

In the above output, both vectors’ first NaN value is present at the same position and removed in the output. The second NaN value is not removed because its corresponding value is not NaN.

Related Article - MATLAB Vector

  • Remove Zeros From a Vector in MATLAB
  • MATLAB Norm() Function
  • 2D Interpolation Using MATLAB