Updated March 10, 2023
Introduction to Matlab nan
In Matlab, a function that represents the values that are not real or not a complex number is known as the NaN function. NaN function means not a number with a special value that containing expressions like inf/inf or 0/0.
Syntax
- X = NaN
- X = NaN(n)
- X = NaN(sz)
- X = NaN(___,’like’,p)
How Nan Function Work in Matlab?
In Matlab, to write a special value that is not a real number or complex number, we used the “Matlab NaN function”. To use the Matlab NaN function, we can simply call the appropriate syntax. To know how the “NaN function” actually works in Matlab, we can see some examples of the NaN function.
Examples
Different examples are mentioned below:
Example #1
Let us see an example related to the Matlab NaN function. In this example, we can see how to assign the NaN values with the Matlab NaN function as we know that in Matlab, a NaN function can be used to get special values like Y = 0/0. For this, we used a syntax that is A = NaN. It is the syntax of the Matlab NaN function; here, A is the variable name where NaN values can assign.
Code:
clc;
clear all;
close all;
A = NaN
Output:
Command window:
Figure 1
After executing the 1st Matlab code, the NaN value is assigned to the variable A, and it is shown in figure 1.
Example #2
Let see another example of the NaN function; in this example, we can see how to create the 4×4 matrix of special values that cannot be a real number or complex number. So as we know, that ‘NaN function’ returns a special value which containing expressions like inf/inf or 0/0. Hence we can use Matlab ‘NaN’ function for creating the 4×4 matrix of special values that cannot be a real number or complex number. Matlab NaN function is written Special values as NaN So for creating a 4×4 matrix; we used the syntax of NaN function. B = NaN(4) gives the 4×4 matrix, which contains special values that cannot be a real number or complex number. Here B, is the name of the matrix. After executing the Matlab code, we get the 4×4 matrix which congaing the special values.
Code:
clc;
clear all;
close all;
B = NaN(4)
Output:
Command window:
Figure 2
We got a 4×4 matrix of special values by using the NaN function, and the matrix is shown in figure 2.
Example #3
Let us see another example related to the Matlab NaN function. In this example, we can see that how we can create an array of specific sizes of special values that cannot be real numbers or complex numbers. So first, we take one array containing the data. After that, we can measure the size of the matrix, and last we used a syntax of the NaN function. Here we take array ‘D’. A array containing the data [1 5 ; 2 6 ; 3 7 ; 4 8 ]. Now we can measure the size of the array. sz = size(D) measures the size of the array ‘D’, and the measured size is stored into the ‘sz’ variable. So as we know, that ‘NaN function’ returns a special value which containing expressions like inf/inf or 0/0. Hence we can use Matlab ‘NaN’ function for creating the 4×4 matrix of special values that cannot be a real number or complex number. Hence we can use the NaN function of creating a matrix of special values that cannot be a real number or complex number. A = NaN(sz) it returns the matrix of size contains in the variable sz. After executing the code, the matrix is created into the command window.
Code:
clc;
clear all;
close all;
D = [1 5; 2 6; 3 7; 4 8];
sz = size(D);
A = NaN(sz)
Output:
Command window:
Figure 3
Example #4
In this example, we can see how to specify the data type of NaN values. So first, we take array D, which has a data type that is double. Now we can use the syntax of the NaN function to create an array of NaN values with a specific function. D = double([1 2 3]) this is the array we can use in the example. Now we can use Y = NaN(size(D),’like’,D) syntax to create array of NaN values with specific function. In this syntax, size(D) measures the size of array D. The above syntax creates an array of size as same as the size of array D, and it also specifies the data type as like array D that is ‘double’. After executing, the code array Y of NaN values is created with the specific data type.
Code:
clc;
clear all;
close all;
D = double([1 2 3]);
Y = NaN(size(D),'like',D)
Output:
Command window:
Figure 4
Advantages
- Easy to create an array of “NaN” values in Matlab.
- Easy to do any arithmetic operations involving NaN values like Y = 0/0.
- We can directly assign the “NaN” values to a variable using the proper syntax.
Conclusion
In this article, we saw the concept of Matlab NaN. Basically, It represents the values that are not real or not a complex number. Nan function plays a vital role in the description and operations of the database because if there are no values present in the database, then it will produce an error.
Recommended Articles
This is a guide to Matlab nan. Here we discuss How Nan Function Work in Matlab and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –