Categories
1. Some Basic Tips

Matlab Transpose

Transpose is used in mathematics to interchange the rows and columns of the input matrix. So, if we have a 2 x 3 matrix as our input, the transpose function will give us a 3 x 2 matrix as the output. In Matlab, we use the ‘transpose function’ to compute the transpose of a matrix or a vector. For a vector with ‘n’ elements, the transpose function gives a ‘n x 1’ matrix as output (‘n’ rows and 1 column).

Syntax of transpose function:

T = transpose (M)

T = M.’

Explanation:

  • T = transpose (M) is used to compute the transpose of the input matrix ‘M’, i.e., it will interchange the rows and columns of the matrix ‘M’.
  • T = M.’ is another way of computing the transpose. It will give the same output as the above syntax.

Examples of Matlab Transpose

Given below are the examples of Matlab Transpose:

Example #1

In this example, we will use the transpose function to compute the transpose of a 2 x 2 real matrix.

Below are the steps to be followed:

  • Initialize the input matrix.
  • Pass this input matrix as an argument to the transpose function.

Code:

M = [6 -5; 1 6] [Initializing the 2 x 2 input matrix]

T = transpose (M)
[Using the transpose function to compute the transpose of the input matrix]

Input:

Matlab Transpose 1

Output:

Before transpose:

Matlab Transpose 2

After transpose:

Matlab Transpose 3

As we can see in the output, the transpose function has interchanged the rows and columns of our input matrix.

Example #2

In this example, we will use the transpose function to compute the transpose of a 3 x 3 real matrix.

Below are the steps to be followed:

  • Initialize the input matrix.
  • Pass this input matrix as an argument to the transpose function.

Code:

M = [2 -1 4; 1 16 2; 0 -4 3] [Initializing the 3 x 3 input matrix]

T = transpose (M)
[Using the transpose function to compute the transpose of the input matrix]

  • Input:
Matlab Transpose 4

Output:

Before transpose:

Matlab Transpose 5

After transpose:

Matlab Transpose 6

As we can see in the output, the transpose function has interchanged the rows and columns of our input matrix.

In the above 2 examples, our input matrix was of real elements.

Next, let us take an example where our input matrix will have complex elements as well.

Example #3

In this example, we will use the transpose function to compute the transpose of a 3 x 3 complex matrix.

Below are the steps to be followed:

  • Initialize the input matrix with complex elements.
  • Pass this input matrix as an argument to the transpose function.

Code:

M = [2+3i 1-4i 1; 1+3i 0 3-2i; 0 3 2] [Initializing the 3 x 3 input matrix with complex elements]

T = transpose (M)
[Using the transpose function to compute the transpose of the input matrix]

Input:

3 x 3 complex matrix

Output:

Before transpose:

Before

After transpose:

After

As we can see in the output, the transpose function has interchanged the rows and columns of our input complex matrix.

In the above 3 examples, we have used a square matrix as our input.

Next, we will use a non-square matrix as input to the transpose function.

Example #4

In this example, we will use the transpose function to compute the transpose of a 2 x 3 real matrix.

Below are the steps to be followed:

  • Initialize the input matrix.
  • Pass this input matrix as an argument to the transpose function.

Code:

M = [1 6 4; 1 4 -2] [Initializing the 2 x 3 input matrix]

T = transpose (M)
[Using the transpose function to compute the transpose of the input matrix]

Input:

2 x 3 real matrix

Output:

Before transpose:

Before transpose

After transpose:

Matlab Transpose 12

As we can see in the output, the transpose function has interchanged the rows and columns of our non-square input matrix.

In the above 4 examples, we have used a matrix as our input to the transpose function.

Next, we will use the transpose function to compute the transpose of a vector input.

Example #5

In this example, we will use the transpose function to compute the transpose of a vector with 5 elements.

Below are the steps to be followed:

  • Initialize the input vector with 5 elements.
  • Pass this input vector as an argument to the transpose function.

Code:

M = [8 -11 4 5 7] [Initializing the input vector with 5 elements]

T = transpose (M)
[Using the transpose function to compute the transpose of the input vector] Please note that since our input has 5 columns and 1 row, our output will have 5 rows and 1 column.

Input:

of a vector with 5 elements

Output:

Before transpose:

Before

After transpose:

Matlab Transpose 15

As we can see in the output, the transpose function has interchanged our input vector rows and columns.

Categories
1. Some Basic Tips

Matlab quadprog

The following article provides an outline for Matlab quadprog. The quadprog or quadratic programming is used to minimize or maximize the input objective function subjected to various constraints. Quadratic programming is used in mathematics to find a vector ‘x’, minimising a quadratic function defined as minx{1/2 * x^T *Hx + f^T * x}.

Or

Matlab quadprog 1

The following constraints are applied to the quadratic function:

  • Ax ≤ b (inequality constraint)
  • Ax = b (equality constraint)
  • LowerBound ≤ x ≤ UpperBound (bound constraint)

In the real world, quadratic programming is used to solve problems, including power generation, portfolio management, design optimization, etc.

Syntax of quadprog Function

Given below is the syntax mentioned:

x = quadprog (H, f, A, b)

Explanation:

  • x = quadprog (H, f, A, b) is used to minimize the quadratic function 1/2 * x^T * Hx + f^T * x subjected to the constraints A*x ≤ b.

Note that the input matrix ‘H’ must be a positive definite for quadratic programming if we need our problem to give a finite value minimum.

Examples of Matlab quadprog

Given below are the examples of Matlab quadprog:

Example #1

This example will use the quadprog function to minimize the objective function “1/2 * x^T *Hx + f^T * x”. For this example, we will use a 3 x 3 input matrix.

Below are the steps to be followed:

  • Initialize a 2 x 2 input matrix ‘H’ for the input objective function.
  • Initialize the vector ‘f’ for the input objective function.
  • Declare the required constraints.
  • Use the quadprog function to minimize the objective function.

Code:

H = [1 3; -1 2] [Initializing the matrix ‘H’ for the input objective function]

f = [-1; 3] [Initializing the vector ‘f’ for the input objective function]

A = [1 0; -1 -2; 0 1];
b = [2; 2; 3];
[Declaring the constraints] [FinalPoint, FunctionVal, ExitFlag, Op, L] = quadprog(H, f, A, b);
[Using the quadprog function to get the required values for the objective function]

FinalPoint, FunctionVal, ExitFlag

Input:

Matlab quadprog 2

Output:

Output 1:

“1/2 * x^T *Hx + f^T * x

Output 2:

Matlab quadprog 4

As we can see in the output, our objective function is now minimized under constraints defined by us. Also, we have obtained the Exit Flag as 1, which implies we have obtained a local minimum for our objective function.

Example #2

This example will use the quadprog function to minimize the objective function “1/2 * x^T *Hx + f^T * x”. For this example, we will use a 3 x 3 input matrix.

Below are the steps to be followed:

  • Initialize a 3 x 3 input matrix ‘H’ for the input objective function.
  • Initialize the vector ‘f’ for the input objective function.
  • Declare the required constraints.
  • Use the quadprog function to minimize the objective function.

Code:

H = [1 1 0; 1 1 1; 1 0 1] [Initializing the matrix ‘H’ for the input objective function]

f = [-1; 1; 1] [Initializing the vector ‘f’ for the input objective function]

A = [1 1 2; 1 -2 1; 1 2 1];
b = [1; -1; 1];
[Declaring the constraints] [FinalPoint, FunctionVal, ExitFlag, Op, L] = quadprog(H, f, A, b);
[Using the quadprog function to get the required values for the objective function]

Input:

Matlab quadprog 5

Output:

Output 1:

we will use a 3 x 3 input matrix

Output 2:

Matlab quadprog 7

As we can see in the output, our objective function is now minimized under constraints defined by us. Also, we have obtained the Exit Flag as 1, which implies we have obtained a local minimum for our objective function.

In the above 2 examples, the input matrix ‘H’ was convex, i.e. positive definite, and so we got a local minimum. If our matrix is not convex, we will not get a local minimum.

Example #3

This example will use the quadprog function to minimize the objective function “1/2 * x^T *Hx + f^T * x”. For this example, we will use a 3 x 3 input matrix.

Below are the steps to be followed:

  • Initialize a 3 x 3 input matrix ‘H’ for the input objective function, such that the matrix is not positive definite.
  • Initialize the vector ‘f’ for the input objective function.
  • Declare the required constraints.
  • Use the quadprog function to minimize the objective function.

Code:

H = [1 2 0; 3 1 2; -1 0 1] [Initializing the matrix ‘H’ for the input objective function]

f = [-1; 1; 1] [Initializing the vector ‘f’ for the input objective function]

A = [1 1 2; 1 -2 1; 1 2 1];
b = [1; -1; 1];
[Declaring the constraints] [FinalPoint, FunctionVal, ExitFlag, Op, L] = quadprog(H, f, A, b);
[Using the quadprog function to get the required values for the objective function]

Input:

Matlab quadprog 8

Output:

Output 1:

The problem is non-convex

Output 2:

minimize the objective function

As we can see in the output, the Exit Flag is-6, which implies we have not obtained any local minimum for our objective function.

Categories
1. Some Basic Tips

Matlab Standard Deviation

Standard deviation is used to measure the spread in statistics. Spread, also referred to as “Dispersement”, tells us how much our data points are spread out around the average or mean. A ‘Bell curve’ is used to study the standard deviation in Statistics. In Matlab, we use the ‘std’ function to compute the standard deviation of a vector or a data set. In today’s world, the standard deviation is extensively used in data analytics to create sophisticated Artificial Intelligence based algorithms.

Syntax of standard deviation function:

SD = std (X)

SD = std (X, w)

Explanation:

  • SD = std (X) is used to compute the standard deviation of the elements of ‘X’. Here, ‘X’ can be a vector, matrix, or multidimensional array.
  • SD = std (X, w) is used to compute the standard deviation of the elements of ‘X’ with a weightage of ‘w’.

Please note that, by default, the standard deviation will be normalized to N-1, N being our number of observations.

Examples of Matlab Standard Deviation

Given below are the examples of Matlab Standard Deviation:

Example 1:

In this example, we will use the std function to compute the standard deviation of elements of an array.

Below are the steps to be followed:

  • Initialize the input array.
  • Pass this input array as an argument to the standard deviation function.

Code:

X = [7 -5 11 2 6 5 -8 0 17 5] [Initializing the input array with 10 elements]

SD = std (X)
[Using the std function to compute standard deviation for the elements of the input array]

Input:

Matlab Standard Deviation 1

Output:

Matlab Standard Deviation 2

As we can see in the output, 7.2877 is the standard deviation of the elements of our input array.

Example 2:

This example will use the std function to compute the standard deviation of 3 x 3 matrix elements.

Below are the steps to be followed:

  • Initialize the input 3 x 3 matrix.
  • Pass this input matrix as an argument to the standard deviation function.

Please note that, for a matrix, the std function will compute the standard deviation of elements along each column.

Code:

X = [5 -5 1; 0 4 3; 8 10 4] [Initializing the input 3 x 3 matrix]

SD = std (X)
[Using the std function to compute standard deviation for the elements of the input matrix]

Input:

3 x 3 matrix

Output:

Matlab Standard Deviation 4

As we can see in the output, we have obtained the standard deviation of the elements of our input matrix along with the columns.

Example 3:

In this example, we will use the std function to compute the standard deviation of elements of a 3-Dimensional array.

Below are the steps to be followed:

  • Initialize the input 3-Dimensional array.
  • Pass this input 3D array as an argument to the standard deviation function.

Code:

X (:, :, 1) = [1 5 3; 2 0 1; 2 2 2];
X (:, :, 2) = [19 3 3; 5 1 3; 3 5 1];
X (:, :, 3) = [0 4 1; 2 -3 1; 2 4 7];
[Initializing the input 3 x 3 matrix]

SD = std (X)
[Using the std function to compute standard deviation for the elements of the input 3D array]

Input:

3-Dimensional array

Output:

Matlab Standard Deviation 6

As we can see in the output, we have obtained the standard deviation of our 3-D input array elements along with the columns.

In the above 3 examples, we have not provided any weightage while computing the standard deviation.

Let us now see how to assign weightage in the std function.

Example 4:

In this example, we will use the std function to compute the standard deviation of a 3 x 3 matrix elements and assign some weightage to it.

Below are the steps to be followed:

  • Initialize the input 3 x 3 matrix.
  • Initialize the weightage vector.
  • Pass the input matrix and weightage vector as arguments to the standard deviation function.

Code:

X = [2 15 4; 1 6 4; 1 -5 2];
[Initializing the input 3 x 3 matrix]

w = [0.7 1 1.5];
[Initializing the weightage vector]

SD = std (X, w)
[Using the std function to compute standard deviation for the elements of the input matrix. We have also passed weightage vector ‘w’ as the second argument]

Input:

Matlab Standard Deviation 7

Output:

assign some weightage

As we can see in the output, we have obtained the standard deviation of our 3 x 3 matrix elements with assigned weightage.

Categories
1. Some Basic Tips

Matlab disp

Disp function is used in MATLAB to display the output of any code without displaying the input variables. This function can be used in cases where our code is not very long or easy to understand, and there is no need of displaying the input variables.

For example, if we use the disp function to display a string ‘MATLAB Disp function’ stored in a variable ‘A’, our output will be ‘MATLAB Disp function’. However, if we do not use the disp function, our output will be A = ‘MATLAB Disp function’.

Syntax of the disp function:

disp (A)

Description of the disp function:

disp (A) function is used to display or print the value stored in the variable ‘A’ without printing the name of the variable

Examples of Matlab disp

Let us now understand the code to use the disp function in MATLAB.

Example 1:

In this example, we will use the disp function to display elements of a vector. Below are the steps to be followed:

  1. Initialize the input vector
  2. Use the disp function to display the elements of the vector

Code:

A = [3 7 1 8 10] [Initializing an input vector and storing it in a variable ‘A’] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]

This is how our input and output will look like in MATLAB:

Input:

Matlab disp output 1

Output:

Matlab disp output 2

As we can see in the output, we have obtained the value (i.e. elements in our case) stored inside the variable ‘A’ by using the disp function.

Matlab disp output 3

If we print the output without using the disp function, this is how it will look like:

Please note that in this case, even the variable name is displayed in the output.

Example 2:

In this example, we will use the disp function to display elements of a matrix. Below are the steps to be followed:

  1. Initialize the input matrix
  2. Use the disp function to display the elements of the matrix

Code:

A = [3 3 1; 8 0 4; 1 4 9] [Initializing a 3 x 3 input matrix and storing it in a variable ‘A’] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]

This is how our input and output will look like in MATLAB:

Input:

Matlab disp output 4

Output:

As we can see in the output, we have obtained the value (i.e. elements of the matrix in this example) stored inside the variable ‘A’ by using the disp function.

output 5

If we print the output without using the disp function, this is how it will look like:

Please note that in this case, even the variable name is displayed in the output.

output 6

Example 3:

In this example, we will use the disp function to display a string. Below are the steps to be followed:

  1. Initialize the input string
  2. Use the disp function to display the string

Code:

A = "Let us learn MATLAB disp function"
[Initializing a string and storing it in a variable ‘A’] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]

This is how our input and output will look like in MATLAB:

Input:

output 7

Output:

output 8

As we can see in the output, we have obtained the value (i.e. a string of words in this example) stored inside the variable ‘A’ by using the disp function.

If we print the output without using the disp function, this is how it will look like:

output 9

Please note that in this case, even the variable name is displayed in the output.

Example 4:

In this example, we will use the disp function to display the output of a mathematical expression. Below are the steps to be followed:

  1. Initialize the variables involved in the expression
  2. Use the disp function to display the output

Code:

X = 5
[Initializing the 1st input variable] Y = 10
[Initializing the 2nd input variable] Z = 25
[Initializing the 3rd input variable] A = X * Z / Y
[Mathematical expression to be solved] disp (A)
[Using the disp function to display the value stored in the variable ‘A’]

This is how our input and output will look like in MATLAB:

Input:

output 10

Output:

output 11

As we can see in the output, we have obtained the value of our output stored inside the variable ‘A’ by using the disp function.

If we print the output without using the disp function, this is how it will look like:

output 12

Please note that in this case, even the variable name is displayed in the output.

Categories
1. Some Basic Tips

Matlab readtable

Read table function in MATLAB is used to create a new table by reading data present in the form of columns in a file. The file from where the read table function can read the data can be a text file, a comma-separated or csv file, or some other excel workbook. The functionality of creating a table by reading from a text file can also be achieved in MATLAB using the in-built Import tool. Please note that we will be utilizing in-built MATLAB files for this article. You can use the same code to create tables from files stored in your local machine. In this topic, we are going to learn about Matlab readtable.

Syntax of read table function:

R = readtable (file)

R = readtable (file, x, y)

Description of the syntax:

  1. R = readtable (file) is used to create a table in MATLAB by reading the data in the file called ‘file’. Here ‘file’ can be a text, csv, or any other excel file with column-oriented fields.
  2. R = readtable (file, x, y) is used to create a table in MATLAB by reading the data in the file called ‘file’. Here the extra arguments passed are to control the type of table we create from the input data file. We can control parameters like formats of the columns, number of rows or columns we need

Examples of Matlab readtable

Let us now understand the code to create a user-defined function in MATLAB

Example 1:

In this example, we will create a table in MATLAB by reading the data from an in-built text file using the readtable function. Below are the steps to be followed:

  1. Initialize the function readtable with the file to be read as an input argument
  2. For this example, we will use the in-built text file called ‘grades.txt.’

Code:

R = readtable (‘grades.txt’)
[Initializing the readtable function and passing the in-built file ‘grades.txt’ as the argument] [readtable function will read this text file and will give a table as an output]

This is how our input and output will look like in MATLAB:

Input:

Matlab readtable output 1.1

Output:

Matlab readtable output 1

As we can see in the output, the readtable function has read the grades.txt text file and given us a table as the output.

In the above example, the text file has only 4 rows and 4 columns; our output table is very small. However, there might be cases where we have a very large file, and we need to view only a small portion of it as a table.

Example 2:

In this example, we will create a table in MATLAB by reading the data from an in-built text file using the readtable function. The file we will be using in this example has 123523 rows and 29 columns. Therefore, we will first create a table for this entire file, and in the next example, we will see how to get a sub-set of this table. Below are the steps to be followed:

  1. Initialize the function readtable with the file to be read as an input argument
  2. For this example, we will use the in-built csv file called ‘airlinesmall.csv

Code:

R = readtable (‘airlinesmall.csv)
[Initializing the readtable function and passing the in-built file ‘airlinesmall.csv’ as the argument] [readtable function will read this csv file and will give a table as an output]

This is how our input and output will look like in MATLAB:

Input:

Matlab readtable output 2.1

Output:

output 2

As we can see in the output, the readtable function has read the airlinesmall.csv file and given us a table as the output. Since the output table is too large to fit in one screen, we have the scroll option in the output.

Next, we will learn how to get a sub-set of this table as the output.

Example 3:

In this example, we will use the same ‘airlinesmall.csv’ file to create a table in MATLAB but will create a sub-set of the entire table. As we noticed in the output in the previous example, the number of rows and columns in the ‘airlinesmall’ data were 123523 and 29, respectively. Therefore, we will now create a sub-table with 5 rows and 4 columns. Below are the steps to be followed:

  1. Initialize the function readtable with the file to be read as an input argument
  2. Here also we will use the in-built csv file called ‘airlinesmall.csv
  3. Pass the range which we need as the argument

Code:

R = readtable (‘airlinesmall.csv)
[Initializing the readtable function and passing the in-built file ‘airlinesmall.csv’ as the argument] R (1:5, 1:4)
[Passing the range, we need as argument]

This is how our input and output will look like in MATLAB:

Input:

output 3.1

Output:

output 3

As we can see in the output, the readtable function with the range passed as an argument has read the airlinesmall.csv file and given us a sub-table as the output.

Categories
1. Some Basic Tips

Matlab Round

‘Round off’ is performed in mathematics to restrict the number of significant digits. For example, the value of ‘pi’ is 3.14159265358979323, and using this entire value every time in calculations can become cumbersome and unnecessary. To avoid this, we round off the values to the required number of decimal places. This is the reason why we mostly see the value of pi as 3.14 (rounding off the value to 2 decimal places).

In MATLAB we use ‘round’ function for the purpose of rounding off the numeric values.

Syntax:

R = round (A)

R = round (A, N)

Description:

  • R = round (A) will round off ‘A’ to the nearest integer. In the case of an array, all the elements of ‘A’ are rounded off to the nearest integer
  • R = round (A, N) will round off ‘A’ till ‘N’ digits. If N < 0, the rounding off is done to the left of the decimal

Examples of Matlab Round

Let us now understand the code of round function in MATLAB using different examples:

Example 1:

In this example, we will take a 3 x 3 matrix. For our first example, we will follow the following steps:

  1. Initialize the input 3 x 3 matrix
  2. Pass the matrix as an input to the round function

Code:

A = [3.11  4.015  11.092;  -3.45  10.8  3.001;  4.981  2.012  4.1];[Declaring the 3 x 3 input matrix]

R = round(A)[Passing the input matrix to the round function] [Mathematically, if we round off all the elements in the above matrix, we will get the following values:

3     4      11

-3    11     3

5     2      4]

Input:

A = [3.11  4.015  11.092;  -3.45  10.8  3.001;  4.981  2.012  4.1];
R = round(A)

Output:

Matlab Round-1.1

As we can see in the output, we have obtained the rounded off values of all the elements in the matrix.

Example 2

In this example, we will take a 2 x 2 matrix. For this example, we will follow the following steps:

  1. Initialize the input 2 x 2 matrix
  2. Pass the matrix as an input to the round function

Code:

A = [1.11  0.01;  -4.45  12.803];[Declaring the 2 x 2 input matrix]

R = round(A)[Passing the input matrix to the round function] [Mathematically, if we round off all the elements in the above matrix, we will get the following values:

1     0

-4    13]

Input:

A = [1.11  0.01;  -4.45  12.803];
R = round(A)

Output:

Matlab Round-1.2

As we can see in the output, we have obtained the rounded off values of all the elements in the matrix. In the above 2 examples, we saw that all the values were rounded off to the nearest integers. Next, we will learn how to round off the values to the required number of decimal places.

Example 3

In this example, we will take a 2 x 2 matrix and will round off each element to 1 decimal place. i.ethere will be one digit after the decimal in the output. For this example, we will follow the following steps:

  1. Initialize the input 2 x 2 matrix
  2. Pass the matrix as the first argument to the round function
  3. Pass ‘1’ as the second argument to the round function

Code:

A = [1.1134  0.09341;  4.43415  1.8103];[Declaring the 2 x 2 input matrix]

R = round(A, 1)[Passing the input matrix to the round function. The second argument, ‘1’ is passed to specify the number of digits we need after the decimal point] [Mathematically, if we round off all the elements in the above matrix till 1st decimal place, we will get the following values:

1.1   0.1

4.4    1.8]

Input:

A = [1.1134  0.09341;  4.43415  1.8103];
R = round(A,1)

Output:

1.3

As we can see in the output, all the elements in the matrix are rounded off till the first decimal place]. Next, we will learn how to round off digits to the right of the decimal point

Example 4

In this example, we will take a 2 x 2 matrix and will round it off to the one-point right of the decimal place. i.e all digits to the right of ‘tens digit’ will become zero or will be rounded off. For this example, we will follow the following steps:

  1. Initialize the input 2 x 2 matrix
  2. Pass the matrix as the first argument to the round function
  3. Pass ‘-1’ as the second argument to the round function

Code:

A = [321.1134  20.09341;  34.43415  14.8103];[Declaring the 2 x 2 input matrix]

R = round(A, -1)[Passing the input matrix to the round function. The second argument, ‘-1’ will round off all the digits before tens place] [Mathematically, if we round off all the elements in the above matrix before tens place, we will get the following values:

320    20

30    10]

Input:

A = [321.1134  20.09341;  34.43415  14.8103];
R = round(A, -1)

Output:

1.4

As we can see in the output, all the elements in the matrix are rounded off and digits before tens place have become zero]

Categories
1. Some Basic Tips

Matlab quantile

Quantile function is used in MATLAB to divide a sample into adjacent, equal-sized subgroups. Quantile is also referred to as ‘fractile’, and in statistics, it is used to divide a given probability distribution into small areas which have equal probability.

The median in statistics, for example, is a quantile which is placed inside a probability distribution such that half of the data is less than it and the other half is more than it. So, it divides a probability distribution into 2 equal areas and is called a 2-quantile.

Syntax of the quantile function:

A = quantile (nr, prob)

A = quantile (nr, X)

Details of the quantile function:

  1. A = quantile (nr, prob) is used to return quantiles for the elements present in a vector or array for the probability ‘prob’, which lies in the range [0,1].
  2. A = quantile (nr, X) is used to return the quantiles for ‘X’ equally placed cumulative probabilities. Mathematically, this is given by (1 / (X + 1), 2 / (X + 1), …, X / (X + 1)) for integer X > 1

Examples of Matlab quantile

Let us now understand how to use the quantile function in MATLAB.

Example #1

This example will use the quantile function to find a quantile for 12 normally distributed numbers. We will use the ‘normrnd’ function of MATLAB to get these normally distributed numbers. Below are the steps to be followed:

  1. Use normrnd function to get 12 random normally distributed numbers
  2. Pass these numbers to the quantile function to get the quantile. For this example, we will calculate the 0.4 quantile

Code:

nr = normrnd (0, 1, 1, 12)[Initializing 12 normally distributed random numbers]

A = quantile (nr, 0.40)[Passing the numbers generated above as input to the quantile function to get the 0.4 quantile]

This is how our input and output will look like in MATLAB:

Input:

Matlab quantile output 1

Output:

Matlab quantile output 2

As we can see, we have obtained 0.4 quantile for our 12 normally distributed random numbers. This means that 40% of our numbers are below -0.0987, and the rest 60% are above it.

Example #2

In this example, we will use the quantile function to find quantiles for a matrix. We will use a 3 x 3 matrix of probabilities and will calculate quantiles, first for its columns and then for its rows. We will use the ‘normrnd’ function of MATLAB to get these normally distributed numbers as the elements of the matrix. Below are the steps to be followed:

  1. Use the normrnd function to get random normally distributed elements for the matrix.
  2. Pass these numbers to the quantile function to get the quantiles for columns. For this, we will have to pass the ‘dim’ argument as 1, which represents ‘columns.’
  3. Pass these numbers to the quantile function to get the quantiles for rows. For this, we will have to pass the ‘dim’ argument as 2, which represents ‘rows.’

Code to calculate quantile for columns:

nr = normrnd (0, 1, 3, 3)[Initializing a 3 x 3 matrix with normally distributed elements]

A = quantile (nr, 0.40, 1)[Passing the matrix created above as input to the quantile function to get the 0.4 quantile. Please note that we have passed ‘1’ as the 3rd argument, which represents that the calculation is to be performed along the column]

This is how our input and output will look like in MATLAB:

Input:

Matlab quantile output 3

Output:

Matlab quantile output 4

As we can see, we have obtained 0.4 quantile for the columns of our 3 x 3 matrix of normally distributed random numbers.

Code to calculate quantile for rows:

A = quantile (nr, 0.40, 2)[Passing the matrix created above as input to the quantile function to get the 0.4 quantile. Please note that we have passed ‘2’ as the 3rd argument, which represents that the calculation is to be performed along the row]

This is how our input and output will look like in MATLAB:

Input:

output 5

Output:

output 6

As we can see, we have obtained 0.4 quantile for the rows of our 3 x 3 matrix of normally distributed random numbers.

Example #3

In this example, we will use the quantile function to calculate evenly distant quantiles. We will use the ‘normrnd’ function of MATLAB to get 8 normally distributed numbers.

Code:

nr = normrnd (0, 1, 1, 8)[Initializing 8 normally distributed random numbers]

A = quantile (nr, 3)[Passing the numbers generated above as input to the quantile function to get 3 evenly distant quantiles]

This is how our input and output will look like in MATLAB:

Input:

output 7

Output:

output 8

As we can see, we have obtained 3 evenly distant quantiles for our 8 normally distributed random numbers.

Categories
1. Some Basic Tips

Matlab Lists

MATLAB Lists can be ordered, unordered, multi-level, and can be created and formatted using the DOM API in a program that generates a report. A list can be created from an array string in MATLAB, which specifies the items in the list or creates a list with items inserted one by one. It is easy to create a list using an array, creating the list by inserting items one by one is handy when there are multiple elements in the item, like in the case of a table or paragraph.

Syntax:

  1. In MATLAB, we represent lists as mlreportgen.dom.OrderedList or mlreportgen.dom.UnorderedList objects
  2. mlreportgen.dom.ListItem objects are used to represent items of the lists

Description:

  1. mlreportgen.dom.OrderedList is used to create an ordered list
  2. mlreportgen.dom.UnorderedList is used to create an Unordered or bulleted list

Let us now understand the code to create a list in the MATLAB report.

Examples of Matlab Lists

Lets us discuss the examples of Matlab List.

Example 1:

In the first example, we will create an unordered list in a MATLAB report. The list will be created from an array string. Below are the steps that we will follow for this example:

  1. Import the library mlreportgen.dom.*
  2. Initialize the document where we want to create the list
  3. Use the append method to append the document with the list items

Code:

import mlreportgen.dom.*

[Importing the required library]

Doc = Document("Employees", "html");

[Initializing the variable ‘Doc’ and storing the report to be appended in it]

App = append(Doc, ["Tom", "Jack", "Berlin", “Ryan”, “Harris”]);

[Appending the report ‘Employees’ using an array string]

close(Doc);

[Closing the report once it is appended]

rptview(Doc);

[Opening the report “Employees” to check if the list is created]

This is how our input and output will look like in the MATLAB command window:

Input:

import mlreportgen.dom.*
Doc = Document("Employees", "html");
App = append(Doc, ["Tom", "Jack", "Berlin", "Ryan", "Harris"]);
close(Doc);
rptview(Doc);

Output:

matlab list 1

As we can see in the OUTPUT, the report “Employees” is now appended with an unordered list as expected.

Example 2:

Let us take another example where we create an unordered list in a MATLAB report. The list will be created from an array string. Below are the steps that we will follow for this example:

  1. Import the library mlreportgen.dom.*
  2. Initialize the document where we want to create the list
  3. Use the append method to append the document with the list items

Code:

import mlreportgen.dom.*

[Importing the required library]

Doc = Document("Organization", "html");

[Initializing the variable ‘Doc’ and storing the report to be appended in it]

App = append(Doc, ["TCS", "Reliance Industries", “HCL", “Capgemini”]);

[Appending the report ‘Organization’ using an array string]

close(Doc);

[Closing the report once it is appended]

rptview(Doc);

[Opening the report ‘Organization’ to check if the list is created]

This is how our input and output will look like in the MATLAB command window:

Input:

import mlreportgen.dom.*
Doc = Document("Organization", "html");
App = append(Doc, ["TCS", "Reliance Industries", "HCL", "Capgemini"]);
close(Doc);
rptview(Doc);

Output:

matlab list 2

As we can see in the OUTPUT, the report ‘Organization’ is now appended with an unordered list as expected.

In the above 2 examples, we created unordered lists using an array string. Next, we will learn to create an ordered list using an array string.

Example 3:

In this example, we will create an ordered list in a MATLAB report. The list will be created from an array string. Below are the steps that we will follow for this example:

  1. Import the library mlreportgen.dom.*
  2. Initialize the document where we want to create the list
  3. Pass the input array string to the Ordered list constructor
  4. Use the append method to append the document with the list items

Code:

import mlreportgen.dom.*

[Importing the required library]

Doc = Document("Months", "html");

[Initializing the variable ‘Doc’ and storing the report to be appended in it]

NewList= OrderedList(["January", "February", "March", "April", "May"]);

[Creating a new variable and storing an ordered list in it]

append(Doc, NewList);

[Appending the report ‘Months’ using ‘NewList’ created in above step]

close(Doc);

[Closing the report once it is appended]

rptview(Doc);

[Opening the report ‘Months’ to check if the list is created]

This is how our input and output will look like in the MATLAB command window:

Input:

import mlreportgen.dom.*
Doc = Document("Months", "html");
NewList = OrderedList(["January", "February", "March", "April", "May"]);
append(Doc, NewList);
close(Doc);
rptview(Doc);

Output:

As we can see in the OUTPUT, the report ‘Months’ is now appended with an ordered list as expected.

matlab list 3

Example 4

In this example, we will create another ordered list in a MATLAB report. The list will be created from an array string. Below are the steps that we will follow for this example:

  1. Import the library mlreportgen.dom.*
  2. Initialize the document where we want to create the list
  3. Pass the input array string to the Ordered list constructor
  4. Use the append method to append the document with the list items

Code:

import mlreportgen.dom.*

[Importing the required library]

Doc = Document("Countries", "html");

[Initializing the variable ‘Doc’ and storing the report to be appended in it]

NewList= OrderedList(["India", "Australia", "Japan", "USA", "UK"]);

[Creating a new variable and storing an ordered list in it]

append(Doc, NewList);

[Appending the report ‘Countries’ using ‘NewList’ created in above step]

close(Doc);

[Closing the report once it is appended]

rptview(Doc);

[Opening the report ‘Countries’ to check if the list is created]

This is how our input and output will look like in the MATLAB command window:

Input:

import mlreportgen.dom.*
Doc = Document("Countries", "html");
NewList = OrderedList(["India", "Australia", "Japan", "USA", "UK"]);
append(Doc, NewList);
close(Doc);
rptview(Doc);

Output:

example 4

As we can see in the OUTPUT, the report ‘Countries’ is now appended with an ordered list as expected.

Categories
1. Some Basic Tips

Matlab regression

A MATLAB Regression function is used to find the relationship between two variables by putting a linear equation to the observed data. One variable is considered as an explanatory variable and another variable is considered as a dependent variable. The dependent variable is continuous in nature. The dependent variable means variable values are focused or explained and the other hand explanatory variable or independent variable is used to focus on the dependent variable. The dependent variable is denoted as Y and the explanatory variable or independent variable is denoted as X. For a multiple linear regression of the responses in the vector of the explanatory variable on the predictors in the matrix of independent variable Matlab Regression function is used.

Syntax

The syntax for MATLAB Regression function is as follow: –

b = regress(y,X)

How does Regression work in Matlab?

A MATLAB Regression function is used to find the relationship between two variables by putting a linear equation to the observed data. There are two variables one is the explanatory or independent variable and another variable is the dependent variable. The dependent variable is continuous in nature. There are very simple steps for knowing how regression function works in Matlab and the steps are as follows; –

Steps 1: Create one variable as an explanatory or independent variable and load all input

data.

Steps 2: Create one more variable as a dependent variable and load the all data.

Steps 3: Then write the equation which can be including the slope of the line.

Steps 4: Then use MATLAB Regression function with proper syntax

Steps 5: Execute the Matlab code to get the output.

Examples of Matlab regression

Here are the following examples mention below

Example 1:

Let us see the example of Matlab regression and how it works in Matlab. As we know for the regression process in Matlab we used the MATLAB Regression function. So let assume the number of observation is 100. Then we create the artificial noise using the rand function. “noise = randn(n,1);” this line is used to create the artificial noise. After that we create the independent variable x. “x=rand(n,1).*10;” this line creates the independent variable x which is artificially enlarged than noise. After that we ready to write the equation.  “y= 2+3.5*x+noise;” this line gives the equation, here 3.5 is the slope of independent variable x. And we add the artificial noise in this equation. Then we plot the graph using the plot function between independent variable x and dependent variable y. After that, we used “lsline” syntax for creating a line or plotting the line. After that, we create the first beta values by attaching the ones. By using “X= [ones(size(x)) x];” this line we create the first beta values and then finally we create the final beta by using the MATLAB  Ra egression function. “beta = regress(y, X)” this syntax creates the beta. So after executing the Matlab code we get 2 beta values.

Code:

clc;
clear all;
close all;
n =100;
noise = randn(n,1);
x=rand(n,1).*10;
y= 2+3.5*x+noise;
plot(y,x,'.')
lsline
X= [ones(size(x)) x];
beta = regress(y,X)

Output:

Command window:

Matlab regression output 1

Figure 1

After executing the Matlab code we get regression values using the regression function.

Example 2:

Let us see one more example of regression. As we know to find regression in Matlab we used the MATLAB Regression function. In this example, we see that the efficient way of regression function in Matlab.  Here a large number of observations is assumed. So let assume the number of observation is equal to 1000. Then the artificial noise is created using the ‘randn’ function “noise = randn(n,1);” this line is used to create the artificial noise in Matlab. Then we create an independent variable and a dependent variable. For creating the independent variable A

“A=rand(n,1).*9;” this line is used.  After that we ready to write the equation “B= 1+3.5*A+noise;”, this line gives the equation, here 3.5 is the slope of independent variable A. And we add the artificial noise in this equation. Then we plot the graph using the plot function between independent variable A and dependent variable B. After that, we used “lsline” syntax for creating a line or plotting the line. After that, we create the first beta values by attaching the ones. By using  “X= [ones(size(A))A];” this line we create the first beta values and creates finally we create the final beta by using the MATLAB  Regression function. “beta = regress(B,X)” this syntax creates the beta. So after executing the Matlab code we get 2 beta values.

Code:

clc;
clear all;
close all;
n =1000;
noise = randn(n,1);
A=rand(n,1).*9;
B= 1+3.5*A+noise;
plot(B,A,'.')
lsline
X= [ones(size(A)) A];
beta = regress(B,X)

Output:

Command window:

Matlab regression output 2

Figure 2

After executing the Matlab code we get regression values using the regression function. 

Categories
1. Some Basic Tips

Matlab Table

In Matlab ‘Table,’ function is used to create the table. A table can contain different type’s data or information such as variables, values, constants, etc. It also has different sizes as long as all variables. The table function arranges the data into rows and columns as we define.

Syntax:

  • T = table(var1,…,varN)
  • T = table(___,’VariableNames’,varNames)
  • T = table(___,’RowNames’,rowNames)

How to Create a Table in Matlab?

The Table function is used for creating the table. A table can contain different types’ data and sizes.  So we need to take that all data and then create a table with different sizes and different data types.  To create a table the following steps are used.

Step 1: Read all the data from the file.

Step 2: Assign all data to a variable.

Step 3: Then use the appropriate syntax of the ‘Matlab Table’ function to create a table.

Step 4: Then execute the code.

Examples of Matlab Table

Here are the following examples mention below

Example 1:

Let us consider an example of a Table. In this example, we can see how to create a table using the Table function. We have data on the sale of fruits in five shops. We can create a table by using the Table function. First, we take all sell entries as per fruit name and then we make a table using the Table function. A variable names is taken are Mango, coconut, Banana, and Apple. [8; 4; 3; 10; 9] this data is assigned to the variable Mango. A variable coconut contain [8; 4; 3; 10; 9] data.  [11; 16; 14; 17; 14] that data is assign to variable Banana.  A variable Apple contain [76; 63; 31; 33; 19] data. All this data is the number of fruit boxes sold in 5 shops.  All appropriate data is loaded into the table and with the help of the Table function, a table will be created. The table function arranges all data into rows and columns as we define. Execute the code to create a data

Code:

clc;
clear all;
close all;
Mango = [8;4;3;10;9];
coconut = [8;4;3;10;9];
Banana = [11;16;14;17;14];
Apple = [76; 63; 31; 33; 19];
Shops  = [ 'X'; 'Y'  ; 'F'; 'J'; 'E'; ];
T = table(Shops, Mango, coconut, Banana, Apple)

Output:

Command Window:

Matlab table output 1

Figure 1

After executing the code, we see that a table is created in the command window.

The created table is shown in figure 1.

Example 2:

Let us consider one more example of Table. We have data on certain company’s products and their transport places. And as we know that for creating a table in Matlab we used the Table function. In this example, we can see another type to create a table. In this example, we can assign all the data when we use the ‘Matlab table’ function. We use the table function with proper syntax to create a table. “T = table ( categorical ( {‘D’;’C’;’B’} ), [16;32;8], …{ ‘XY’;’YZ’;’AB’ }, … VariableNames’, {‘Product_Name’,’Quantity’,’State’})” this line is created a table using provide data and variables. Here used variable Names are ‘Product Name’, ‘Quantity’, and ‘State’. The table function arranges all data into rows and columns as we define. After executing the code table will be created.

Code:

clc;
clear all;
close all;
T = table(categorical({'D';'C';'B'}),[16;32;8],...
{'XY';'YZ';'AB'},...
'VariableNames',{'Product_Name','Quantity','State'})

Output:

Command Window:

In the first example, we take all the data first, and then we use the Table function.

Matlab table output 2

Figure 2

Example 3:

Let us see one more example of the table. In this example, we see how to specify row names while creating a table in Matlab. As we know the in Matlab ‘table’ function is used for creating the table. So we use the table function to specify row names while creating a table in Matlab. So we first take all the data into variables. We have some data related to persons like their age, height, weight. So first we take all that data into variables. Age, Weight, Height, and Name these variables to contain all the data. Now we use the table function with proper syntax for creating a table. The table function arranges all data into rows and columns as we define. After executing the code table will be created.

Code:

clc;
clear all;
close all;
Name = {'A';'B';'C';'D';'E'};
Age = [18;19;16;17;18];
Height = [71;69;64;67;64];
Weight = [55;63;56;49;59];
T = table(Age,Weight,Height,'RowNames',Name)

Output:

Command Window:

Matlab table output 3

Figure 3