Updated March 8, 2023
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.
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:
Code:
clc ;
clear all ;
disp ('Output');
Value1 = randn
Value2 = randn
Value3 = randn
Output:
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:
Code:
clc ;
clear all ;
disp('Output');
Value1 = randn (1,3)
Value2 = randn (4,2)
Output:
Code:
clc ;
clear all ;
disp ('Output');
Value1 = randn (1, 3, 2)
Value2 = randn(3, 1, 2)
Output:
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:
Code:
clc ;
clear all ;
disp('Output');
Value1 = randn( 2,'single')
Value2 = randn (2 ,'double')
Output:
Code:
clc ;
clear all ;
disp('Output');
Input = [ 4,3; 2,2; 4,7]
Dim = size (Input)
Value1= randn (Dim)
Output:
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:
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 –