Categories
1. Basics of Matlab

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:

<!-- wp:paragraph -->
<p><code>Value 1 = randn<br>Variable name = randn</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>Value 1 = randn(5)<br>Variable name = randn (no. of rows and columns)</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>Value 1 = randn (1 ,3)<br>Variable name = randn (no. of rows , no. of columns)</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>Value 1 = randn (1, 3, 2)<br>Variable name = randn (no. of rows, no. of columns, no of matrices)</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>Value 1 = randn (2, 'single')<br>Variable name = randn (no. of rows and columns, ‘type’)</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>Value 1= randn ( Dim )<br>Variable name = randn (size)</code></p>
<!-- /wp:paragraph -->

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’

Leave a Reply

Your email address will not be published. Required fields are marked *