Categories
4. Matlab Programs

Cell to String MATLAB

There are two commands used to covet cell data into string format one is char and the other is a string. char and string commands extract all the data from cell arrays and stored in the form of string. In Matlab, we use string notations as data in single or double quotes ( “ ” or ‘ ‘ ). There are some operations and methodologies that cannot be operated on cells or cell arrays but can be operated on strings, in such cases this manipulation is used. There is no limitation to the cell data, it can be a single element or in the form of vectors or in form of multidimensional matrix.

Syntax:

Out = char ( input)

  • Output variable name = char ( input cell name)

Out = string ( input)

  • Output variable name = string ( input cell name)

How does Cell to String Matlab work?

To convert cell data into a string first we need to create cells with some data .cell is created by using curly brackets ( { } ). If data is single-dimensional then elements can be separated by a comma and if data is multi-dimensional then arrays are separated by a semicolon (;) along with elements separated by a comma. After assigning cell we can apply command char or string by using the above syntax format to convert all the data into the string.

Examples to Implement Cell to String MATLAB

Below are the examples mentioned:

Example 1:

In the first example let us assume one input cell as a variable input. One data is assigned to the input which is ‘hello’. At the output side, we can check data is in string format.

Code:

clc ;
clear all ;
input = {'hello'}
out = char(input)

Output:

Cell to String MATLAB - 1

Example 2:

In this example we have assigned colors name array to input, that means in this problem input is cell array or vector which has multiple values in a single dimension. Input array is  { ‘ red ‘ , ‘ blue ‘ , ‘ yellow ‘ , ‘ green ‘ , ‘ black ‘  , ‘ white ‘ } .after applying command we will get output as independent and separate strings of input .

Code:

clc ;
clear all ;
input = { ' red ' , ' blue ' , ' yellow ' , ' green ' , ' black '  , ' white ' }
str = char ( input )

Output:

Cell to String MATLAB - 2

Code:

clc ;
clear all ;
input = {' 3 ' , ' 5 6 ' , ' 4 3 ' , ' 2 2 ' , ' 5 4 ' }
out = char ( input )

Output:

Cell to String MATLAB - 3

Example 3

In this example let us consider input array is a multidimensional matrix with three rows and three columns. input is cell-matrix along with input data in form of alphabets such as { ‘ G ‘ , ‘ F ‘ , ‘ E ‘ ; ‘ R ‘ , ‘ D ‘ , ‘ N ‘ ; ‘ V ‘ , ‘ S ‘ , ‘ C ‘ }

Code:

clc ;
clear all ;
input = { ' G ' , ' F ' , ' E ' ; ' R ' , ' D ' , ' N ' ; ' V ' , ' S ' , ' C ' }
out = char ( input )

Output:

multidimensional matrix

Example 4

Now let us consider input in form of all the data types such as int, float, char, and string. in this example input is assigned with a single dimensional array or vector with data ‘ HELLO ‘, ‘ 356 ‘, ‘ HI ‘, ‘ 22.5 ‘,’ B Y E ‘, ‘ 10 ‘ }. in this HELLO, HI and BYE are string datatype. 365 and 10 are integers and 22.5 is float.

Code:

clc ;
clear all ;
input = {' HELLO ' , ' 356 ' , ' HI ' , ' 22.5 ' ,' B Y E ' , ' 10 ' }
out = char( input )
out ( 3 )
out(5)

Output:

data types
Categories
4. Matlab Programs

Factorial in Matlab

MATLAB provides us with plenty of functionalities, useful in various computational problems. In this article, we will study a powerful MATLAB function called ‘MATLAB factorial’.

Before we understand how to calculate factorial in MATLAB, let us first refresh our understanding of factorial. Factorial of any positive integer ‘n’ is a product of all the whole numbers from 1 to n (both included). In Mathematics, an exclamation sign is used to represent the factorial of a number.

Syntax of Factorial Function in Matlab

f= factorial(n)

Description of Factorial Function in Matlab

Here is the description mention below

1. f = factorial(n)

  • Here n is a non-negative integer value and this function will result in a product of all positive integers whose value will either be equal to ‘n’ or less than ‘n’
  • Let us consider ‘n’ to be an array, then function ‘f’ will result in factorial of all values which are part of array ‘n’.The size of the output array and its data type will be same as of input array ‘n’
  • Generally, it is represented as n! in maths but in MATLAB it will be written as f =factorial(n)

Now let us understand the computation of factorial in MATLAB with various examples:

Let us first take a simple example of calculating the factorial of a whole number.

Example 1:

f = factorial(5)

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

Factorial in Matlab output 1

So, our output is the product of all whole numbers less than and equal to our input (excluding zero).

f = 5X4X3X2X1

f = 120

Example 2:

f = factorial(3)

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

Factorial in Matlab output 2

Next, we will understand how to take the factorial of an array.

f = 3×2

f = 6

In the following example, let us take a 2 X 2 array.

Example 3:

Input array:[ 5 2 ;7 4]

When our input array is passed to MATLAB, it will calculate factorial of each element in the array individually.

f = [factorial(1) factorial(5) ; factorial(3) factorial(2)]

f = 2×2

f = 1202

5040 24

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

n = [ 5 2 ;7 4];
f = factorial(n)

Factorial in Matlab output 3

As we can observe in our output, MATLAB has calculated factorial of each element present in the array, i.e 120, 2, 5040, 24 for 5, 2, 7, 4 respectively.

Next, let us take a 2 X 3 array

Example 4:

n = [ 1 5 6 ;3 0 1]

Here also, when our input array is passed to MATLAB, it will calculate factorial of each element in the array individually.

f = [factorial(1) factorial(5) factorial(6) ; factorial(3) factorial(0) factorial(1)]

f = 2×3

f = 1 120 720

6 1 1

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

n = [ 1 5 6 ;3 0 1];
f = factorial(n)

output 4

2. For unsigned integer values

f = uintx(factorial(n))

It will convert the factorial n into an unsigned x 8-bit integer. Since the maximum value for an 8-bit integer is 255 so it will take the factorial of an integer whose value is beyond 255 to be 255 only.

Similarly, for x= 16, it will take the highest value to be 16-bit int value that is 65535.

Let us understand with an example:

Example 1:

Here we will use unsigned 8-bit integer.

f= uint8 ([5 1 6])

f= 1X3 unit 8 row vector

f= 120 1 255

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

n = uint8([5 1 6]);
f = factorial(n)

output 5

As we can see in our output, though factorial of 6 is 720 due to unsigned 8-bit integer it is converted to the highest 8-bit value which is 255

Example 2:

In this example, we will take unsigned 16-bit integer

f = uint16 ([5 1 6 9])

f= 1X4 unit 16 row vector

f= 120 1 720 65535

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

n = uint16 ([5 1 6 9]);
f = factorial(n)

output 6

As we can see in our output, though the factorial of 9 is 362880 due to unsigned 16-bit integer, it is converted to the highest 8-bit value which is 65535.

So, as we can see, MATLAB can be used to calculate factorial of any positive integer. Also, we can use unsigned integers as per our requirement. It can also be used to calculate the factorial of elements in an array.

Categories
4. Matlab Programs

Square Root in Matlab

Square root is defined as taking the root of any square of a single element, a matrix or an array. Square root of a number can be positive or negative as a square of a positive number is positive and the square of a negative number is also positive. It is denoted by √ the symbol. Square root is simply the inverse method of squaring. If a^2 is the square integer, then a is defined as the square root of that number.

For example, 16 is a perfect square number and its square root can be 4 or -4. There are many methods that are used in mathematics to find the square root of a number.

Working and Uses of Square Root in Matlab with Examples

Matlab performs all mathematical functions, so there are also methods to find the square root of a number. In Matlab, we use the sqrt () function to find the square root of a number or each element defined in an array. The input arguments that are used in the function can be scalar, vector, array or multi-dimensional array. They can also be positive, negative or complex in nature. If the input is complex or negative in nature, then it results in a complex number.

Syntax:

Y = sqrt(x)

Example 1:

Y = -3:3

So, the input is in the form of 1*7.

-3  -2  -1  0  1  2  3

A=sqrt(Y)

Output:

(0.0000 + 1.7320i) (0.0000 + 1.4142i) (0.0000 + 1.0000i) (0.0000 + 0.0000i) (1.0000 + 0.0000i) (1.4142 + 0.0000i) (1.7320+0.0000i)

Example 2:

Y = -5: -3

So the input is in the form of 1*4

-5  -4  -3

A = sqrt(Y)

Output:

(0.0000+2.2360i) (0.0000+2.0000i) (0.0000+1.7320i)

We know that is the input of an array is negative than it results in a complex number. In the above two examples, we see that the range consists of negative and positive numbers, so the output of it is a complex number. Some operations differ in Matlab as compared to IEEE standard like the square root of negative zero is 0 in Matlab while it is -0 in IEEE, square root of values less than zero results in a complex number in Matlab while the same is not available in IEEE.

If we want to find the square root of only positive integers in an array, then we can use realsqrt () function in Matlab. Unlike sqrt () function, it gives error messages when we pass input as a negative or complex number. So, if we want to view the result of a negative or complex number than it is preferable to use sqrt () function. The output size and input size should be the same if we use realsqrt ().

Example 3

Input is a 4*4 matrix named as A

Square Root in Matlab-1.1

Y=realsqrt(A)

Output:

Square Root in Matlab-1.3

In the above example, it produces the square root of each element in a matrix. The input arguments can be a matrix, array, vector, scalar, or a multi-dimensional array and they should be positive and real integers. There are various properties of a square root in Matlab which should be noted:

The square root of any even number that is a perfect square should always be even.

For example: 16,36,64,100 etc.

Here 16, 36, 64 and 100 are all even numbers that are a perfect square and the square root of those numbers are 4,6,8 and 10 which are also even numbers.

The multiplication of square roots of the same number results in a positive integer while square roots of the number can also be multiplied and provide the output.

For example: √4 * √4 = 4

√3 * √2=√6

The square root of any odd number that is a perfect square should always be odd.

For example: 25,9,49,81

Here 25,9,47,81 are all perfect squares of odd numbers and the square root of those numbers are 5,3,7,9 which are also odd numbers. The unit digit of an element cannot be 3,2,8 or 7 to be a perfect square.