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 randn
 

Matlab randn

Updated March 8, 2023

Matlab randn

 

 

Introduction to Matlab randn

In Matlab ‘randn’ function is used for normal distribution; it gives random values as output. This function works according to arguments which are passed through function definition. We can pass single or multiple values as arguments in randn function. If arguments are not declared, and the randn function is written alone, it will print only one random value. We can also mention the size we wish to give in function; it can be single dimensional or multi–dimensional. Along with size, we can also mention what kind of distribution we want, single or double or distributed.

Watch our Demo Courses and Videos

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

Syntax:

Value 1 = randn
Variable name = randn

Value 1 = randn(5)
Variable name = randn (no. of rows and columns)

Value 1 = randn (1 ,3)
Variable name = randn (no. of rows , no. of columns)

Value 1 = randn (1, 3, 2)
Variable name = randn (no. of rows, no. of columns, no of matrices)

Value 1 = randn (2, 'single')
Variable name = randn (no. of rows and columns, ‘type’)

Value 1= randn ( Dim )
Variable name = randn (size)

Value 1 = 2 + i * randn
Variable name = Integer + complex variable * randn

How randn Function Work in Matlab?

  • randn functions work on random values as it provides random numeric values to the user.ir creates random values of any type like integers, floating numbers, complex numbers, etc.
  • By default, randn create only one random value, and as per arguments, it can create multiple values.
  • In this function inside the brackets, we can give dimensions of output; it can be single dimension or multi-dimension.
  • Values are distributed as per the user’s requirements.

Examples of Matlab randn

Given below are the examples of Matlab randn:

Example #1

Let us consider one simple example; in this example, value 1 is an input variable that stores random values. There are no arguments passed through the function so that it will print only one value illustrated in an example. If we write the randn function repeatedly in the same code, it will print different random values. For example, a function written three-time therefore, it gives three random values as output.

Code:

clc ;
clear all ;
disp('Output');
Value1 = randn

Output:

Matlab randn 1

Code:

clc ;
clear all ;
disp ('Output');
Value1 = randn
Value2 = randn
Value3 = randn

Output:

Matlab randn 2

Example #2

In this example, we will see dimensions as arguments in randn function. For example, we can see one value is passed through a function. If there is only one value present inside the brackets, then it represents a number of rows as well as a number of columns of the output matrix. Therefore output dimensions are two by two and four by four. For example, there are two values written inside the brackets. This first value represents a number of rows, and the second value represents a number of columns. And there are three values present inside the function brackets; the first value represents a number of rows, the second value represents a number of columns, and the third value represents a number of matrices illustrated in the example.

Code:

clc ;
clear all ;
disp('Output');
Value1 = randn(2)
Value2 = randn(4)

Output:

Matlab randn 3

Code:

clc ;
clear all ;
disp('Output');
Value1 = randn (1,3)
Value2 = randn (4,2)

Output:

Matlab randn 4

Code:

clc ;
clear all ;
disp ('Output');
Value1 = randn (1, 3, 2)
Value2 = randn(3, 1, 2)

Output:

Matlab randn 5

Example #3

Example three shows the type of data we wish to create. Along with dimensions, we can also mention the type of input we want in output like single, distributed or double, which is illustrated in the example. If we perform matrix operation, then as per the rule, all the matrices should be of the same size; therefore, to maintain the size of a matrix, we can use ‘size’ syntax in the randn function, which is shown in the example.

Code:

clc ;
clear all ;
disp('Output');
Value1 = randn(2, 'single')
Value2 = randn (3, 'distributed')

Output:

shows type of data we wish to create

Code:

clc ;
clear all ;
disp('Output');
Value1 = randn( 2,'single')
Value2 = randn (2 ,'double')

Output:

Command Window

Code:

clc ;
clear all ;
disp('Output');
Input = [ 4,3; 2,2; 4,7] Dim = size (Input)
Value1= randn (Dim)

Output:

Matlab randn 8

Example #4

In the previous example, input data were real numbers, but we can also work on complex numbers in randn function. In this example, we multiply by the complex variable ‘i’ to create a number as a complex number. As well as we performed an operation on random numbers, this is illustrated.

Code:

clc ;
clear all ;
disp('Output');
Value1 = 2+ i * randn
Value2 = randn + i * randn

Output:

we multiply by complex variable ‘i’

Conclusion

In this article, we have seen how to use the ‘randn’ function with different arguments. randn function works on arguments, or we can say dimensions which we passed through function by using brackets. This function works exactly like ‘rand’ function; the only difference is rand function create random values between interval o to 1.

Recommended Articles

This is a guide to Matlab randn. Here we discuss the introduction, how randn function work in MatLab? and examples, respectively. You may also have a look at the following articles to learn more –

  1. Laplace Transform MATLAB
  2. Matlab Forms
  3. Signal Processing Matlab
  4. Matlab Autocorrelation

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
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?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW