Matlab Matrix

Matlab Matrix

What is a Matrix?

a rectangular array of expressions arranged in particular rows and columns that is treated as a solitary substance and controlled by specific standards.

MatlabMatrix

A Framework called matrix, is a 2D exhibit of components or elements. The matrix is made by appointing the array components that are delimited by spaces or commas and utilizing semicolons to check the finish of each column. To comprehend it better, presently we have to look at certain guides.

Let us take matrices A1 and B1. In this case A1 is a m x n matrix and B1 is a n x p matrix, they could be duplicated together to create a m x n matrix C. Matrix concatenation is conceivable just if the quantity of sections n in A is equivalent to the quantity of lines n in B.

Working with matrix

Creating a matrix

The below script file shows us how to create a matrix j and i, using square braces and semi colons.

 % to create a matrix
 %j represents a Number Matrix
 j = [0 8 3;4 5 6;7 3 9]
 %String Matrix
 i = ['play;'home'] 

Output: we have created two matrices, one is 3x3 number matrix and another is a 2x4 string matrix.

Knowing the matrix size

To get the size of the matrix we simply use size() function, and pass the matrix as argument to the size() function.

The below script file shows us how to know the size of the matrix x1 and y1, using size function.

 % to know the size of a matrix
 % Creating matrices x1
 x1 = [91 2 13 4;41 5 6 71;79 8 9 10];
 xSize = size(x1)
 y1=['play';'home'];
 ySize = size(y1) 

Output:

If we run the above script file we obtain size of the matrix, this function returns no of rows and columns. In our example, size of x1 is 3x4 and size of y1 is 2x4 is returned.

Referencing the matrix

 To reference matrix elements, we write matrix(m, n). Here rows and columns indexes are represented by m and n. 

Let us drivedeep to know how it works through examples running on command line of Matlab environment.

 % to access
 % a particular element
 % Creating matrices x
 i = [11 21 31 4;4 51 6 71;7 8 9 10];
 i(3,2)
 y=['play';'home'];
 y(1,2) 

Output:

We tried to access (3,2) element of matrix i, and (1,2) element of matrix y, we successfully executed the code.

Delete column or row

Suppose we want to remove a particular column or row from our matrix; we can modify it according to our requirements. As we see in the mentioned example, we first created a matrix X1, and then we tried to delete 3rd row of the matrix.

The below script file shows us how to delete a row from a matrix X1.

 % to delete
 % a row or a column
 % Creating matrices
 X1 = [18 22 31 42;42 52 63 37;17 18 9 10];
 % Deleting 3rd row of
 % a matrix
 X1(3,:) = []
 % Deleting 3rd column in
 % every row of a matrix
 X1(:,3) = [] 

Output: As we can use our code can reflected in our output, 3rd row has been deleted from the matrix X1.

Operations on matrix

Matlab gives inbuilt usefulness to making the matrix out the qualities to it. There are a few numerical and geometrical calculations upheld by Matlab programming language. A portion of the number juggling procedure on the matrix in Matlab are multiplication, addition, subtraction, transpose, complex, power.

Operation nameDescription  
Cross()To perform cross product of matrices
Dot()To perform dot product of matrices
TransposeTo perform transpose of matrices
ComplexTo perform complex operation of matrix
PowerTo perform power operation of matrices

1.Cross product

o = cross(A1,B1) 

If A1 and B1 are vectors, they are of same size.

If A1 and B1 matrices of same size are created. The cross() function treats A1 and B1 as three-element vectors. The cross product of two matrices A1 and B1 is calculated by cross() function of corresponding vectors.

Example:

 A1 = randi(11,4,3)
 
A1 =

     2    11    11
     5     8     8
    11     1     9
     9    10     9
 B1 = randi(24,4,3)
 
B1 =

    10     1    20
    16     7    17
     5     2     8
    17     3    23
 %The cross product of A and B is calculated here.
 o = cross(A1,B1)
 
o =

   209    70  -108
    80    43   -93
   -10   -43    17
   203   -54  -143 

2. Dot product

o = dot(A1,B1)

If A1 and B1 are vectors, having the same length.

If A1 and B1 are multidimensional arrays or matrices, of same size. The dot function treats A1 and B1 as vectors and then calculates the dot product, by maintaining the size of first array dimension not equal 1.Let us explore it further through examples running on command line of Matlab environment.

Example:

Let us create two multidimensional arrays A1 and B1 and perform the desired operation to obtain the required output.

 A1 = cat(3,[1 1;1 1],[2 3;4 5],[6 7;8 9])
 A1 =
 A1(:,:,1) =
      1     1
      1     1
 A1(:,:,2) =
      2     3
      4     5
 A1(:,:,3) =
      6     7
      8     9
 B1 = cat(3,[2 2;2 2],[10 11;12 13],[14 15; 16 17])
 B1 =
 B1(:,:,1) =
      2     2
      2     2
 B1(:,:,2) =
     10    11
     12    13
 B1(:,:,3) =
     14    15
     16    17 

3.Transpose

B1 = A1.'

This returns the transpose (nonconjugate)of A1. Transpose means interchanging the row and column index for each corresponding element. In case of complex elements, then A1.', sign of the imaginary parts is not effected. For instance, if have taken  A1(3,2) is 1+2i and B1 = A1.', then B1(2,3) is also 1+2i.

Let us execute on command line of Matlab environment and check out.

Example:

 A1 = magic(4)
 
A1 =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1
 B1 = A1.'
 
B1 =

    16     5     9     4
     2    11     7    14
     3    10     6    15
    13     8    12     1 

4.comple matrix

Example

 A = [1 3 4-1i 2+2i; 0+1i 1-1i 5 6-1i]
 A = 2×4 complex
  
    1.0000 + 0.0000i   3.0000 + 0.0000i   4.0000 - 1.0000i   2.0000 + 2.0000i
    0.0000 + 1.0000i   1.0000 - 1.0000i   5.0000 + 0.0000i   6.0000 - 1.0000i
  
 B = A.'
 B = 4×2 complex
  
    1.0000 + 0.0000i   0.0000 + 1.0000i
    3.0000 + 0.0000i   1.0000 - 1.0000i
    4.0000 - 1.0000i   5.0000 + 0.0000i
    2.0000 + 2.0000i   6.0000 - 1.0000i 

5.Matrix multiplication

Example:

 A = [1 3 5; 2 4 7];
 B = [-5 8 11; 3 9 21; 4 0 8];
 Calculate the product of A and B.
 C = A*B
 C = 2×3
  
     24    35   114
     30    52   162 

6.Power

To find power of elements of a matrix, we can simply use “^” symbol or sometimes we use the function mpower() to find the power of a matrix.

Example:

 q = [1 2; 3 4];
 z = q^2
  
 z = 2×2
  
      7    10
     15    22 

7.Square root

Matrix representation is created of the fourth difference operator, A1. Symmetric and positive definite are two attributes mandatory for the matrix created.

Let us explore it further through examples running on command line of Matlab environment.

 A1 = [51 -4 11 10 0; -14 16 -4 11 0; 11 -4 6 -4 21; 20 21 -24 6 -24; 20 0 21 -4 26]
 
A1 =

    51    -4    11    10     0
   -14    16    -4    11     0
    11    -4     6    -4    21
    20    21   -24     6   -24
    20     0    21    -4    26 

Calculate the unique positive definite square root of A using sqrtmX is the matrix representation of the second difference operator.

 X1 = round(sqrtm(A1))
 
X1 =

   7.0000 + 0.0000i   0.0000 + 0.0000i   1.0000 - 1.0000i   1.0000 + 0.0000i   0.0000 + 0.0000i
  -1.0000 + 1.0000i   4.0000 + 1.0000i  -1.0000 + 0.0000i   1.0000 - 1.0000i   1.0000 - 1.0000i
   1.0000 + 0.0000i   0.0000 + 0.0000i   2.0000 + 2.0000i  -1.0000 + 0.0000i   3.0000 - 1.0000i
   3.0000 - 2.0000i   3.0000 - 2.0000i  -2.0000 + 2.0000i   2.0000 + 2.0000i  -3.0000 + 1.0000i
   2.0000 + 0.0000i   0.0000 + 0.0000i   3.0000 - 1.0000i   0.0000 + 0.0000i   4.0000 + 1.0000i 

7.Page transpose

Example

Create a 3-D array Z, and pagetranspose() is used to transpose array’s each page.

Let us explore it further through examples running on command line of Matlab environment.

 o = repelem(1:3,3,1);
 Z = cat(3,o,2*o,3*o)
 Z =
 Z(:,:,1) =
      1     2     3
      1     2     3
      1     2     3
 Z(:,:,2) =
      2     4     6
      2     4     6
      2     4     6
 Z(:,:,3) =
      3     6     9
      3     6     9
      3     6     9
 Y = pagetranspose(A)
 Y=
 Y(:,:,1) =
      1     1     1
      2     2     2
      3     3     3
 Y(:,:,2) =
      2     2     2
      4     4     4
      6     6     6
 Y(:,:,3) =
      3     3     3
      6     6     6
      9     9     9