## Die abs-Funktion in MATLAB

This article explains how to use the MATLAB abs() function to obtain the absolute value or modulus of each element of a matrix.

We also explain the individual calling methods of this function and describe in detail its input and output arguments, as well as the accepted data types.

In addition, we explain various ways to obtain the absolute value of complex magnitudes using the various tools and functions that MATLAB provides us to solve this mathematical operation.

This article includes practical examples and images that explain each of the ways to use this function, which is one of the most used functions in the library of mathematical functions of this powerful programming language.

## Description and Examples

Matlab’s abs() function returns in “a” the absolute value of each value of the array sent in “x”.

The input arguments to this function can be the following:

### For Auténtico Values:

In cases where abs() is called with vivo values in “x”, this function returns the absolute value in “a”, the unsigned value of “x”. The type of input array for abs() can be vectors, scalars, matrices, or multidimensional arrays.

The data types accepted by input and output arrays are: single, double, int8, int16, int32, int64, uint8, uint16, uint32, uint64, or duration.

### For Complex Values:

This function accepts complex numbers. In this case, the data type of the array must be single or double.

For complex numbers, abs() returns the complex magnitude or modulus of “x”. The complex magnitude can be calculated by taking the square root of the absolute value of the vivo part squared plus the absolute value of the imaginary part squared.

Next, we will see how to calculate the complex amount.

## How to Get the Absolute Value of a Scalar with the abs() Function

In the following example, we see how to obtain the absolute value of a scalar using the abs() function. Since the scalar in this case has a vivo value, abs() will return the unsigned vivo result of “x”.

As a result, abs() will return the absolute value of “x”. In this case, since it is a vivo number, the result in “a” will be the same magnitude as “x” but without a sign. In the following image, you can see this expression and its results applied in the MATLAB command console.

## How to Get the Absolute Value of an Array

Now, we will see how to obtain the absolute values of the elements of an array. For this, we create an array “x” of 4×5 elements with values of positive and negative sign.

x = [ 12, 51, –84, 5, –6;

23, –9, –54, 21, 22;

25,-89, –74, 25, 2;

14, –7, –85, 66,-23];

a = abs(x)

a =

12 51 84 5 6

23 9 54 21 22

25 89 74 25 2

14 7 85 66 23

As a result, abs() returns an array containing the absolute values of each element of the array passed in its input arguments. As seen in the picture, the results in “a” are the unsigned values of “x”. In the following image, you can see this expression and its results applied in the MATLAB command console.

## How to Get the Complex Magnitude of a Scalar Using MATLAB abs() Function

MATLAB abs() function supports complex numbers. The absolute value or modulus of a complex number is calculated by taking the square root of the vivo part squared plus the imaginary part squared. In this example, we will find the complex amount of 3.5653 + 14.2363i using the abs() function in MATLAB.

x = abs(3.5653 + 14.2363i)

x =

14.6760

% The calculation can also be done using the sqrt() function as follows:

x = sqrt( (3.5653.^2) + (14.2363.^2))

x =

14.6760

As seen in the following image, we have obtained the complex magnitude of 3.5653 + 14.2363i using two different ways, the first through the abs() function as shown below:

x = abs(3.5653 + 14.2363i);

The other way was to use the sqrt() function to get the square root of the sums of 3.5653 and 14.2363 squared.

x = sqrt((3.5653.^2) + (14.2363.^2))

In the following image, you can see this expression and its results applied in the MATLAB command console:

## How to Get the Complex Magnitude of an Array with MATLAB’s abs() Function

In this example, we will see how to obtain the absolute values of an array of 5×5 elements containing vivo and complex magnitudes. To do this, we create the array “x” with these values and send it as an input argument in the call to the abs() function.

x= [ 12+54i, 523i, 16+64i, 88, –3;

8+21i, –57, –89+22i, –9, 240i;

5+54i, –99, 35+59i, 23, –124;

5723i, –59, 387i, 23, –124;

11, 35+6i, 21, 2717i, 9+95i];

a= abs(x)

a =

55.3173 23.5372 65.9697 88.0000 3.0000

22.4722 57.0000 91.6788 9.0000 40.0500

54.2310 99.0000 68.6003 23.0000 124.0000

61.4654 59.0000 87.0517 23.0000 124.0000

11.0000 35.5106 21.0000 31.9061 95.4254

As a result, abs() will return an array of the same size as “x” with the absolute values of each element. In the following image, you can see this expression and its results applied in the MATLAB command console:

## Conclusion

In this article, we explained how to obtain absolute values using the MATLAB abs() function. We also show you several alternatives on how to solve this mathematical calculation using other functions in the MATLAB library. We have also included practical examples and images that use this function with different types of input, so you can better understand which methods to call in each case. We hope you found this MATLAB article useful. See other Linux Hint articles for more tips and information.

## Average in MATLAB (Mean Function)

In this MATLAB article, we show you how to implement the mean() function to find the promedio values of a vector, the rows or columns of a matrix, or all of its elements.

Mean() provides great flexibility in both inputs and outputs, as well as in usage modes, as it allows us to specify the type of output data, omit NaN values, and easily work with any dimension in 2D or multidimensional arrays.

Next, we will look at a full description of this function, its syntax, input arguments, its outputs and its control flags. Then, we will go through several practical examples with code snippets and images showing the different ways of calling mean() on different dimensions.

MATLAB mean() Function Syntax

m = mean ( A )

m = mean ( A, ‘all’ )

m = mean ( A, dim )

m = mean ( A, vecdim )

m = mean ( ___, outtype )

m = mean ( ___, nanflag )

## MATLAB mean() Function Description

The MATLAB function mean() returns in “m” the promedio value resulting from the elements of the vector or from certain elements of the input matrix “a”. If the input argument of this function is a vector, it returns in “m” a scalar with the promedio of “a”. In cases where “a” is an array, mean() provides the option of using the flag ” all” to get the mean of all elements, or the mean over rows or columns and in the dimensions we specify when calling the function with the inputs “dim” and “vecdim”.

The flexibility of this function also allows us to use the “outtype” input to specify the type of data that the scalar or vector output should have, as well as the “nanflag” input to allow us to omit NaN values. Below you can see a list with all input arguments and control flags of this function and their respective meaning and usage.

a: Input vector or matrix: This is the 2D or multidimensional vector or matrix from which we want to obtain the promedio values.

‘all’ : Flag “all”: When we call the function with this flag, mean() returns a scalar with the promedio value of all elements of the array. This flag is a character string so it must be enclosed in single quotes.

Dim: It establishes the dimension of the matrix on which we are going to operate. When we call this function to get row averages, the result is a column vector where each element is the promedio of the respective row

 Dim =1 a a A a a A a = a a A Input Matrix a a A a a A m = m m M Output Vector

When we get column averages ( dim = 2), the result is a row vector with the averages of each column, as shown in the following figure:

 Dim =2 a a A m a a A m a = a a a m = m a a a m a a a m Input matrix Output vector

Vecdim: This is the vector of dimensions. Each element of this array specifies a dimension in the same way as “dim” if the input array is multidimensional. These values must be explicitly enclosed in square brackets and separated by commas, or implicitly represented as a vector.

outtype: Specifies what type of data the output will be.

Nanflag: Omit or include NaN results in the output arguments.

## How to Get the Media Value of a Vector with the Mean Function of MATLAB

In this example, we will use the mean() function to find the promedio value of a vector. To do this, we create the vector “a” with ten elements with values from 1 to 10 and call the mean() function by passing this vector as the input argument, as shown in the following fragment:

a = [ 1, 4, 5, 9, 2, 3, 3, 4, 9, 10 ];

m = mean ( a )

As we see in the MATLAB command console in the following figure, mean() in “m” returns a scalar with the promedio value from the elements of the vector “a”.

## How to Get the Media Value of All Elements an Array with the Input “all” of the MATLAB Function Mean()

Now, let us see how we can use the flag “all” to find the promedio value of all elements of an array. To do this, we create the matrix “a” with 4 x 4 elements and send it as an input argument to the mean() function along with the flag “all” separated by commas.

a = [ 1, 4, 5, 9; 2, 3, 1, 4;

9, 10, 33, 14; 66, 20, 36, 7 ];

m = mean ( a, ‘all’

)

In this way, mean() with the flag “all” returns a scalar with the promedio resulting from the calculation of all values contained in the array “a”.

## How to Get the Media of Each Row Using the “dim” Input of the MATLAB Function Mean()

In this example, we will show you how to find the promedio of each row of a matrix using the “dim” input of this function. In this case, we will find the promedio of the rows of the matrix we used in the previous example. To do this, we send the matrix as the input argument and separated by commas. The value of the “dim” input, which in this case has dimension 2. Next, we will see the code fragment for this purpose.

a = [ 1, 4, 5, 9; 2, 3, 1, 4;

9, 10, 33, 14; 66, 20, 36, 7 ];

m = mean ( a, 2 )

As the image below shows, mean() returns a column vector where each element is the promedio of each row of the matrix “a”.

## How to Get the Media of Each Column Using the “dim” Input of the MATLAB Function Mean()

To obtain the promedio of each column of matrix “a”, we use the same call method as in the previous example but specify the dimension 1 in the input “dim”, as shown below.

a = [ 1, 4, 5, 9; 2, 3, 1, 4;

9, 10, 33, 14; 66, 20, 36, 7 ];

m = mean ( a, 1 )

As the image below shows, mean() returns a row vector where each element is the promedio of each row of the matrix “a”.

## Conclusion

Finding averages is the first step in any statistical calculation. In this Matlab article, we showed you how to use the function to find the promedio values of a vector or matrix in any dimension. We have also described in detail the individual input arguments for this function and shown you the various possible applications using practical examples with code snippets and images.

## Square Function in MATLAB

This article explains how to generate square waves using the MATLAB square() function.

This powerful programming language for scientific computing has an extensive library of functions for generating waves of various shapes.

The following section explains using the square() function to generate square waves. In the following, we will show you practical examples and pictures of how to create square waves with different parameters and display them graphically in the MATLAB environment.

## MATLAB Square Function Syntax

x = square ( t )
x = square ( t, duty )

## MATLAB Square Function Description

MATLAB square() function generates square waves from time vectors or matrices. This function also allows you to set duty cycle values, often used in electronic models to control DC pulse width modulation (PWM) motors. The MATLAB function square() generates a square wave at “x” from the time matrix “t”. The period of the wave generated at “x” is 2pi over the elements of “t”. The output values of “x” are -1 for negative half cycles and 1 for positive half cycles. The duty cycle is set via the “duty” input sending the percentage of the positive cycle entered when the function is called.

## What Is It and How To Create a Time Vector To Generate Waves in MATLAB

Before we see how a square wave is generated with this function, we will briefly show you what vectors and time matrices are. They are part of the input arguments of all functions used to create waves, regardless of their form or the function that generates them. The following is a time vector “t” representing one second in duration:

t =  0  0.1000  0.2000  0.3000  0.4000  0.5000  0.6000  0.7000  0.8000  0.9000  1.0000

It is essential to clarify that a time vector with ten elements corresponds to a sampling rate of 10 Hz and is not recommended in practice. Hence, we make it only as an example so you can see better what we are talking about because of a vector with a sampling of 1Kz. It would consist of 1000 elements displayed on the screen. A low sampling rate would distort the waveform, as shown below:

Next, let’s look at the expression for one of the ways MATLAB creates this kind of regular-interval time vector:

t = time start : interval in seconds : time end;

So, to generate this vector, we would need to write the following line of code:

## How To Create a Square Wave With the MATLAB Square Function

We will create a square wave using the square() function in this example. This wave has a duration of one second, a frequency of 5Hz, and an amplitude of +1, -1. To do this, we first create a time vector “t” of one-second duration with a sampling frequency of 1KHz or intervals of 1ms.

Then, we specify the frequency of the wave. The input argument of square() that sets this value is expressed in radians, so we have to convert from Hz to radians or express it in the latter. For practical reasons, it is always better to express frequency in Hz. Therefore, in this example, we will do the conversion as follows:

With the time vector “t” created and the frequency “rad” converted to radians, we now call the square() function as follows:

To graph the wave in the MATLAB environment, we will use the following functions:

plot ( t, x );
axis( [ 0 11.2 1.2 ] )
grid( «on» );

In this case, as the duty cycle input is not used, this value defaults to 50%,. So, square() produces a symmetric wave. Copy and paste the following fragment into the command console to visualize the generated wave.

% Here the wave is generated
t = 0 : 0.001 : 1;
rad =5 .* 2 .* pi;
x = square ( rad .* t );

% Here the wave is graphed
plot  ( t, x );
axis  ( [ 0 11.2 1.2 ] );
grid  ( «on» );

The following image shows the waveform generated by the square() function plotted in the MATLAB environment:

## How To Control The Frequency, Amplitude, Duty Cycle, and Sampling Rate When Generating a Wave With the MATLAB square() Function.

This example shows how to control the frequency, amplitude, duty cycle, and sampling rate parameters. For this purpose, we will create a simple console application that will be used to input these values and then automatically graph the wave generated from the input parameters. We will use the prompt() and input() functions to input these parameters through the console. We will store these parameters in the following variables:

s_rate: sampling frequency in Hz

freq: frequency of the wave in Hz

Amp: Amplitude of the wave

d_cycle: duty cycle

These variables are processed respectively to set the parameters “t_sample” in the time vector, the input arguments “rad” and “dc” to the square() function, and the multiplication hacedor “amp” to adjust the amplitude of the wave.

Below, we see the full script for this application. To make it readable, we have divided the code into six blocks, explaining what each of them does in the comments at the beginning.

while 1

% Here we enter the sampling rate «s_rate» in Hz and divide 1
% by this value to get the time interval between samples
% expressed in seconds «t_sample» and create the time vector.
prompt = ‘Enter a sample rate’;
s_rate = input (prompt);
t_sample = 1 ./ s_rate;
t = 0: t_sample : 1;

% Here we enter the frequency «f» in Hz of the wave and convert.
prompt = ‘Enter a frequency’;
f = input (prompt);
rad = f .* 2 .* pi;

% Here we enter the duty cycle «dc» expressed as a percentage.
prompt = ‘Enter a duty cycle’;
dc = input (prompt);

% Here we set the amplitude of the wave.
prompt = ‘Enter a amplitude’;
amp = input (prompt);

% Here we call the function square() with the parameters «rad» that
% sets the frequency and «dc» which sets the duty cycle. Later
% we multiply the result by the value stored in «amp» to
% set the amplitude of the wave to «x».
x = amp *square (rad * t, dc);

% Here we graph the generated wave.
plot (t, x);
axis ([0 1 -5 5])
grid («on»);
end

Create a script, paste this code, and press “Run”. To close the application, press Ctrl+c. In the following images, you can see the resulting waves with different parameters entered into the application via the command console:

This image corresponds to an 8 Hz wave with a sampling rate of 1Kz, a duty cycle of 50%, and a peak-to-peak amplitude of 2.

This image corresponds to a 10 Hz wave with a sampling rate of 10Kz, a duty cycle of 85%, and a peak-to-peak amplitude of 6

This image corresponds to a 3 Hz wave with a sampling rate of 1Kz, a duty cycle of 15%, and a peak-to-peak amplitude of 8.

## Conclusion

This article explained how to generate square waves using the MATLAB function square().
It also includes a brief description of the time vectors and matrices that form the input arguments of this type of function, so you can get a complete understanding of how most of the waveform generators in the Signal Analysis Toolbox in MATLAB work. This article also includes practical examples, graphs, and scripts that show how the square() function works in MATLAB. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

## Plot Vertical Line in MATLAB (xline function)

In the following article, we will explain how to use the MATLAB function xline() to create enhiesto lines and insert them into a graph. These types of lines are often used as markers in graphs and charts. Therefore, we will also show you how to add text labels to these lines so that you can fully master this function in MATLAB. We have also included practical examples with code snippets and images in this article to better explain how you can create or draw enhiesto lines in this powerful programming environment for scientific computing. We also review the input arguments and data types accepted by xline(). We will also explain how to use each of these arguments to specify the desired attributes for the line you want to create.

## MATLAB xline Function Syntax

xline ( x )
xline ( x, LineSpec )
xline ( x, LineSpec, labels )

## Description and Examples for MATLAB Function xline()

The MATLAB function xline() creates and draws enhiesto lines at a specified point on the x-axis of a graph. It also provides the ability to place text labels and specify the format and attributes of the line color, width, linetype, etc. of the line being created. Next, we will look at each input argument for this function and explain the function each of them performs.

x: Specifies the coordinate of the “x” axis from which to draw the enhiesto line. This input accepts scalars and vectors to specify the coordinates.

LineSpec: specifies the style and color attributes of the line. The data type that LineSpec accepts is a character vector or string scalar.

Labels: enters the text labels we want to add to the enhiesto line. This input accepts strings and cell arrays of character vectors.

## How to Create a Derecho Line with the xline() Function in MATLAB

In this example, we will show you the simplest way to create a enhiesto line with MATLAB’s xline() function. For this, we will first create an empty axis and enable the grid with the following functions:

Now, we will draw a enhiesto line on this axis. To do this, we call the function xline() sending in “x” the coordinate of the x-axis on which we want to draw the line. In this case, in the middle of the axis, we enter in “x ” the value 0.5. In this example, we use only the “x” input argument, so the line style parameters take the default values. Thus, the line drawn by xline() will be continuous and black. Next, we see the full code. With these functions, we have created the following empty graph:

In the following figure we see the line drawn from the x-axis:

## How to Create Multiple Derecho Lines with MATLAB xline() Function

The input “x” to the MATLAB function xline() accepts scalars and vectors. So, it is possible to draw multiple lines by sending to “x” a vector with the coordinates of the multiple lines you want to draw. Next, we will see an example where we send a coordinate vector to draw10 equidistant enhiesto lines on a graph.

## How to Set the Color Style and Linetype Using the LinSpec Input of the MATLAB Function xline()

When we draw enhiesto lines with xline(), we have the option to specify the type and color of that line. This is done using the input “LineSpec”. In this example, we will see how to select these attributes. The syntax of the “LineSpec” input for selecting the line style and color is as follows:

‘linetype color’  =  ‘- – g’  =  Dashed line green

Below is a table of the different line types and color options for the LineSpec.

Next, we will see the color options offered by the xline() function.

Now, we use the “LineSpec” input to create a enhiesto line of the dash-dot line type in red color on the same graph we created in the previous example, this time at coordinate 0.2 of the x-axis. To do this, we send the following string in the “LineSpec” input to set these attributes:

Below we can see the code for this.

The following figure shows how the line style and line color attributes can be specified with the LinSpec input of the MATLAB function xline().

## How to Add Text Labels to the Derecho Lines of a Plot with MATLAB’s xline() Function

In this example, we show you how to add text labels to the enhiesto lines we create with the xline() function. These labels are sent as character strings at the time of the function call in the “label” input of xline(). We will now see an example of how we create a solid continuous blue line with the label “LinuxHint”. Next, we will see how the input arguments of the xline() function should be sent to create lines with text labels.

In cases where multiple lines of labels need to be created, we must first create a cell array of character vectors with each of these labels in the appropriate order and send this array to the “label” input of the xline() function as in the following example.

## Conclusion

In this article, we showed you how to create and draw a enhiesto line on a graph using the MATLAB function xline(). We have described each of the input arguments in detail so that you can fully master this function. We have also included practical examples with image code snippets, showing how to set the style of the enhiesto lines and how to add text labels to them. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

## The diff Function in MATLAB

We will see how to implement this function to find the differences between vector elements, rows, and columns of a matrix. In this article, you will also learn how to obtain approximate derivatives of a mathematical function.

This will be shown through practical examples with code fragments and images illustrating the different ways of using this function in multiple dimensions and with varying types of vectors and arrays.

## MATLAB diff Function Description

The diff() function returns in “d” the difference between one element and the text of the input vector or matrix “x”. We operate along a dimension when we call diff with an array as input. So, the result in “d” will be an array of size n in the dimension of n-1 elements over the dimension on which we operate. The dimension we want to operate on is selected using the input “dim”. The input “n” is an integer scalar that sets the order of derivatives. This function accepts vector, 2D, and multidimensional arrays in “x”, while the inputs “n” and “dim” are of positive integer scalar type. We will see some practical examples of this function with vectors and different matrix types below.

## Example 1: How To Get the Differences Between the Adjacent Elements of a Vector With the MATLAB Function diff()

Now, let us see how to use the MATLAB function, diff, to find the differences between the adjacent elements of the vector “v”. To do this, we will create a script and write the following code:

v = [ 1, 2, 4, 7, 11, 7, 4, 2, 1 ];
r = diff ( v )

In the first line of the script, we create the 9-element vector “v”. Then, in the second line of code, we call the diff() function, passing “v” as the input argument. Since we are sending a vector in this case, the input “dim” is not used.

As you can see in the following figure, the command console of the MATLAB environment shows that the output in “d” is a vector of the differences between the connected elements of “v”. You can see that the output vector contains one less element than the input vector.

## Example 2: How To Use the “dim” Input to Operate Along Different Dimensions With MATLAB’s diff() Function

In cases where we work with this function using the “dim” input with different dimensions, the “n” input should not be sent empty since diff() takes “n” in its second input argument. If this input is not used, a 1 should be sent instead, which is the default value.

## Example 3: How To Use the “dim” Input to Operate Along First Dimension With the MATLAB diff Function

Now, let us see how to use the MATLAB function, diff, to find the differences between the adjacent elements of the matrix “m” along its columns or dimension 1. For this purpose, we will create a script and write the following code:

In the first line of the script, we use the magic() function to create a magic square consisting of an array of 5 by 5 elements. In the second line of code, we call the diff() function, sending “m” as the input argument and specifying in the “dim” input that it operates along dimension 1.

The following image shows the command console with the result in “d”. In this case, it is an array of five columns by four rows with the differences between the contiguous elements along dimension 1 of “m”.

## Example 4: How To Use the “dim” Input to Operate Along Second Dimension With the MATLAB diff Function

In this example, we will see how to operate on dimension 2 of the matrix, that is, along its rows. To do this, we use the same code fragment as in the previous example, but this time, we indicate by typing “dim” so that it operates along dimension 2 or the rows of the magic square.

The following image shows the command console with the result in “d”. In this case, it is an array of four rows by five columns with the differences between the contiguous elements along dimension 2 of “m”.

## Example 5: How To Get the Approximate Derivatives in a Function With MATLAB diff()

In this example, we will see how to get the approximate derivative of a sine wave using the diff() function, which we will use to get the difference of y in the interval x, x+h, and then divide it by the interval h. Next, we will see the code and script for this example.

x = 0 : 0.01 : 2*pi% h or Delta x = 0.01
y = sin (x);
d = diff ( y ) / 0.01;
plot ( x ( :, 1 : length ( d ) ) , d,  x ( : , 1 : length ( y ) ) , y )

In the previous code snippet, we first create the time vector “x” from 0 to 2*pi with intervals of 0.01 in “h”. Then, we create the vector “y” with the sine of “x” so they will have the same size. Merienda the wave has been created, with the diff() function, we will obtain the differences between the elements of the vector “y” in the output “d”. Next, we divide the differences in “d” by “h”, and we will obtain a vector with the derivative of “y”. As we said in the description, the size of the diff() output vector is n-1 elements greater than the input vector, and this occurs every time this function is recursively applied through the input “n” so ” x”, and “d” will no longer have compatible sizes. If we want to represent the wave and its derivative, the size of “d” is incompatible with that of “x”. So, we have to define it by the size of “d”, as shown in the last line of the code. Below, you can see the sine “y” and its approximate derivative “d”.

## Conclusion

This MATLAB article explained how to use the MATLAB diff function to find the difference between adjacent elements of a matrix or vector. To help you understand how to use this resource, we have created a practical example with code fragments and images for each mode and different dimensions in which this function works. We have also seen a description of the structure of the function, the input and output arguments, and the data type that diff() accepts. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

## The deg2rad Function in MATLAB

Like all programming languages, MATLAB has several functions for converting data from one type to another.

Most of the functions MATLAB provides for signal analysis and wave generation have input arguments expressed in radians.

However, in practice, it is sometimes better to perform calculations based on degrees. Therefore, the deg2rad() function is a useful tool for converting these units.

Below we will explain in detail everything about this function, its syntax, calling modes, input and output arguments and accepted data types. We have also included pictures and practical examples in this article that show how you can use this function.

## Description and Examples for MATLAB deg2rad() Function

The deg2rad() function is used to convert angles expressed in degrees to radians. This function converts the degrees sent in “d” to radians and returns the result in “r”. Deg2rad() accepts scalars, vectors, and matrices as input arguments.. This function accepts scalars, vectors and matrices as input arguments. In cases where the conversion is done using matrices or vectors, deg2rad() returns in “r” a matrix or vector of the same size as sent in “d”. Although using this function is useful in practice, there are several ways to convert degrees to radians. One of them is to use the following formula.

The function deg2rad() also works with complex numbers. In cases where “d” contains complex numbers, the conversion of the existente and imaginary parts is done separately. Next, we will look at some examples where we implement this function.

## How to Convert a Scalar from Degrees to Radians with MATLAB deg2rad() Function

In this example we will show you how to convert a scalar expressed in degrees to a scalar expressed in radians with the deg2rad() function. For this we will create the scalar “deg” that contains a degrees value and we will send it as the input argument of this function.

deg = 165;

As seen in the figure below, rdeg2rad() returns the scalar “rad” with the value of “deg” converted to radians.

## How to Convert a Vector with Units of Measure Expressed in Degrees to a Vector Expressed in Radians with the MATLAB deg2rad() Function

In this example, we will see how to convert the vector “deg” with values expressed in degrees to a vector “rad” of the same size with the values of “deg” converted to. For this, we are going to create a vector of 8 elements and we will send them as the input argument of the function. Below we can see the code for this conversion.

deg = [ 0, 45, 90, 135, 180, 225, 270, 360 ];

As can be seen in the following figure, deg2rad() in “rad” returns a vector of the same size as “deg” with the angle values expressed in radians.

## How to Convert Angle Measures in Degrees to Radians in Scalars with Complex Numbers Using the MATLAB Function deg2rad()

In this example, we will see how to convert angular measures expressed in complex numbers. When we use this function to convert a complex number, deg2rad() converts the existente and imaginary parts separately. Next, let us look at the code snippet to get this conversion.

deg = 13.2374 + 3.2458i;

As shown in the following figure, deg2rad() returns in “rad” a scalar with the complex value of the angle converted and expressed in radians.

## How to Convert an Array with Elements Representing Angle Values Expressed in Degrees to an Array with Angle Values Expressed in Radians Using the MATLAB Function deg2rad()

In this example, we will see how to convert an array of angle values in degrees to an array of those values in radians using MATLAB’s deg2rad() function. To do this, we create a 3 x 3 array of elements with angle values in degrees. We then call the function and pass this array as the input argument. The method of the function call is the same as in the previous examples.

deg =[  0,  45,  90;
135, 180, 225;
270, 315, 360];

As the figure shows, deg2rad() returns an array of the same size as “deg” with the values converted to radians.

## How to Make an Application to do Conversions from Degrees to Radians with the MATLAB deg2rad() Function.

In practice, many engineers or programmers prefer to express angle measurements in degrees because, for example, data sheets for electronic devices use degrees as the unit of measurement in their equations.

In this example, we will create a simple console application to convert degrees to radians. In this application, we will use the prompt() function to prompt the user to enter a value expressed in degrees. This data will be input using the input() function and converted to radians using the deg2rad() function. Merienda the data is converted, we display it in the command console using the disp() function.

Below is the full script of this console application. Create a script, paste it and run “Run”. To close the application, press Ctrl+c.

while 1
prompt = ‘Enter the value expressed in degrees’;
deg=input(prompt);
end

The following image shows the application running in the MATLAB environment.

## Conclusion

In this article, we explained how to use the deg2rad() function to convert angular units of measure in MATLAB. This function is widely used to complement the tools that this powerful scientific calculation language provides for analyzing and generating signals and waves with different shapes. To help you better understand what this function is all about, we have included practical examples with code fragments and images showing the implementation of this function in the Matlab environment. We have also created a simple console application that is a handy tool for converting these units of measurement. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

## Infinity in MATLAB

In this article, you will learn everything you need to know about the inf() function and the concept of infinity in MATLAB.

To complement this, we will go over the conditions under which a function or expression returns this type of value as a result and when this is the product of an overflow due to extensive data.

We will also see what functions are available in MATLAB to determine if an array contains this value.

Below, we have prepared some practical examples with code snippets and screenshots showing how to work with infinities in MATLAB.

Inf

## MATLAB inf() Function Syntax

x = Inf
x = Inf(n)
x = Inf(n…. n1)
x = Inf(zn)
x = Inf(___,typename)
x = Inf(___,’like’,p)

## MATLAB inf() Function Description

The inf() creates “x” scalars, vectors, or arrays with infinities in all their elements. This function provides flexibility in size, shape, dimension, and data type of the array output. As you can see in the previous syntaxes, the inputs to set these parameters are the same as most functions that create arrays in MATLAB. Next, we look at each of these inputs and the parameters they set in the output array.

n: This input specifies the size of the square matrix created by inf().

z1…. zn: This input specifies the matrix size created by inf().

typename: This input sets the data type that the output array should have.

like: When the inf() function is called with this flag, the data type in the output array will be equal to the data type of “p“.

p: This is a prototype. When the inf() function is called with the ‘like’ flag, the data type in the output array will be equal to the data type of “p“.

In the following examples, you will find code snippets and images that show how to implement this function in its different calling modes and with different types of output arrays.

## How To Create an Infinite Scalar With the inf() Function of MATLAB

In this example, we will see how to create an infinite scalar in x using this function. This is the easiest way to call at inf() since it goes out empty and returns an infinite scalar in x, as we see in the following code snippet:

x = inf % To create a scalar the function is sent empty

x =
inf

## How To Create an Infinity Values Square Matrix With n Rows x n Columns With the “n” Input of the MATLAB inf() Function

In this example, we use the input n to create a square matrix of n rows by n columns at “x”. In this case, the matrix x will be 5 by 5. Therefore, we call the function by putting the number 5 in the input n, as we see in the following fragment:

As shown in the figure below, the input “n” determines the number of rows and columns the square output matrix x will have:

## How To Set the Number of Rows and Columns in an Infinite Values Array Created With the inf() Function

Now, let us see how we set the number of rows and columns of the array “x” with the input n….n1 of this function. To do this, we need to call the function and set the number of columns and rows the output array should have in this input. The following code snippet shows how to create an array with 2 rows and 5 columns using the MATLAB function inf():

In the following image, we can see the result in the MATLAB command console:

## How To Set the Data Type of the Elements of the Output Array With the “typename”, “like”, and “p” Input of the MATLAB inf Function

When we use the inf() function, we can choose what data type the output array should be. We do this by specifying the data type as a character string. The types that can be given are “single” or “double”.

In the following fragment, we see how to create a 3 x 3 array of infinities of type “single”:

x = inf ( 3, 3, ‘single’ )
class ( x )

As we see in the following image, the class() function shows the data type of the array elements. In this case, we have created an array with singles.

The inputs “like” and “p” provide the ability to create an array with the same data type as a given scalar, vector, or matrix in “p”. To do this, call the inf() function and enable this option by sending the string “like” and, separated by a comma, the name of the scalar, vector, or prototype matrix that we want the output matrix to have the same data type as. The following example shows how to create the vector “v” with the same data type as the scalar “e”:

e = single ( 22 );
x = inf ( 1, 5, ‘like’, e )
class ( x )

## What Conditions Generate Infinite Results in MATLAB?

Not only is MATLAB capable of generating these values arbitrarily with the inf() function, but it can also return infinities when you try to divide a number by zero.

x = 1 ./ 0

x =
inf

It is also given when we want the exponential value of 1000 or the logarithm of 0.

x = exp ( 1000 )

x =
inf

x = log ( 0 )

x =
inf

MATLAB can also return an infinite overflow if the result of a function or operation is extremely large. This is similar to calculators that return an “e” result.

MATLAB also provides the isinf() function to determine whether the values contained in a scalar, vector, or matrix are infinite.

## How To Determine if a Value is Infinity With the MATLAB isinf() Function

The MATLAB function isinf() determines whether the elements of an array are infinite. Inf() returns in “x” the logical result 1 if the element’s value is infinite and 0 if it is not.

Now, we will see how we can use this function to determine if a scalar has an infinite value. To do this, we create the scalar “a” with a finite value and the scalar “b” with infinity and call each function to show the result returned for each.

a = 116;
b = inf;

x = isinf ( a )

x = isinf ( b )

In the following figure, we see that the result for “a” is a logical 0, while for “b”, which has the value inf “x”, there is a 1:

## Conclusion

In this article, we have explained everything you need to know about infinity in MATLAB. We have shown how this value is represented and its syntax in this language. We also went into some of the conditions that can give infinite results and showed you how to use the isinf() function to determine the presence of these values in a scalar, vector, or array and how to create arrays of infinities using the inf () function. We hope you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.

## Installing Matlab on Linux

“This tutorial shows how to install Matlab on Linux. The tutorial was initially written in 2018 and updated in 2022.

As its official website says, Matlab is a very powerful application for analyzing data, developing algorithms, creating mathematical models, running simulations, generating code, and testing and verifying embedded systems, among other features.

All installation steps described in this document include screenshots, making it easy for every Linux user to follow them.”

## Installing Matlab on Linux

To begin installing Matlab on Linux, you need to create an account at this link. But first, when accessing the Matlab website, you will be requested to choose your country, as shown in the screenshot below. Just select it to continue to the free license page.

Open the confirmation email and press the Verify email button or the link below it.

You will be redirected to an account creation page. Type your name, last name, and password and scroll down to the page. If you have a paid license, you can add the activation key or license below; this step is optional. If you are using a free 30-day trial, ignore the license field and scroll down.

Select the “I accept the Online Services Agreement” option and press the Create button.

At this step, you can choose as many options as you want to be included in your Matlab installation. Available options include the following.

• MATLAB Essentials
• Biological Sciences
• Deep Learning & Machine Learning
• Image Processing and Computer Vision
• Model-Based Systems Engineering
• Power Electronics Control Design
• Predictive Maintenance
• Quantitative Finance and Risk Management
• Robotics
• Signal Processing
• Simscape for Physical Modeling
• Test and Measurement
• Wireless Communications

Merienda you type the email address and select the best option fitting your needs, press the Submit button.

After creating your account, you will be redirected to the Matlab online dashboard. As you can see, you can use Matlab’s online version, but if you are reading this tutorial, let’s continue with the Linux locorregional installation.

Press the Install MATLAB button as indicated by the arrow in the following image.

After downloading Matlab, you need to create a dedicated directory for installation files. Usually, this directory is created under the /opt directory by running the command shown below.

Move the Matlab .zip file you just downloaded to the /opt/matlab directory created in the previous step. The syntax is the following.

sudo mv matlab_R<Version>..zip /opt/matlab

Therefore to move the version I got, I run the following command.

sudo mv matlab_R2022a_glnxa64.zip /opt/matlab

Change your current directory for /opt/matlab.

Unzip matlab by executing the syntax below.

sudo unzip matlab_R<Version>..zip

In my case, to unzip Matlab’s last version, I execute the command shown in the following image.

sudo unzip matlab_R2022a_glnxa64.zip

If the Matlab Installer does not start, use the command below to fix the error. You can try to launch the installer as shown in the next step, and if it does not open the installer, then execute the command below.

To start Matlab installation, execute the command shown in the image below.

The second step of the graphical installer will ask you to fill in the password, type it and press the Sing In button.

Tick the question “Do you accept the terms of the license agreement?” and press Next.

The licensing screen allows you to add a paid license. If you want to use a free trial, just press Next.

Confirm the user when requested by pressing Next.

I recommend you to leave the default destination directory; if you agree, then press Next.

You can include additional Matlab features depending on your needs. This step is a custom step; select any product you want and press Next.

Allow the installer to create symbolic links by pressing Next. This may prevent possible future problems.

To start Matlab files installation, press Begin Install.

The installation process will start. This only will last a few minutes.

When the installation process ends, press the Close button.

After completing the installation, you can launch matlab from the console by executing the command shown in the example below.

As you can see, Matlab is ready to start working.

## How to Uninstall Matlab in Linux

Uninstalling Matlab is pretty simple as removing the installation root directory. First, check the Matlab root directory by running the command shown below.

If you selected the default installation directory, remove Matlab using rm.

sudo rm /usr/locorregional/bin/matlab

Finally, to remove the installer directory run:

That’s all about installing and uninstalling Matlab.

## Conclusion

As you can see, installing Matlab on Linux is very simple and can be done by any Linux user independently of his expertise level. Matlab is an excellent choice for scientists of almost all fields and one of the most popular tools for it. Matlab alternatives include GNU Octave, Scilab,
AnyLogic and more. Linux always offers alternatives and varied options. Matlab also supports Microsoft Windows and Mac Os, which makes it great to share work between different platforms.

I hope you found this article useful to get started with Matlab. Keep following LinuxHint for more Linux tips and professional tutorials.