EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab nan
 

Matlab nan

Updated March 10, 2023

Matlab nan

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Matlab nan output 1

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:

Matlab nan output 2

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:

output 3

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:

output 4

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 –

  1. Matlab Object
  2. Strcmp Matlab
  3. Simplify Matlab
  4. MATLAB Unique

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW