A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Saturday, February 20, 2016

Operations on Matrices and Arrays - MATLAB


Arithmetic operations of scalars are discussed in previous post. Now we will discuss about operations of matrices. All the basic operations on matrices, addition, subtraction, multiplication, division, and exponentiation, can be done in MATLAB. Operations other than addition and subtraction are done in two ways, one way, uses the standard symbols (*, /, and ^) where normal matrix operations are performed and the second way, which is called element-by-element operations, use a period typed in front of the standard operation symbol. There is an additional Matlab operator for matrices called left division operators (.\ or \).

Addition and Subtraction

Matrix addition can be accomplished only if the matrices to be added have the same dimensions for rows and columns.  If A and B are two matrices of the same size, then the sum A+B is the matrix obtained by adding the corresponding entries in A and B.
If A and B are matrices with the same size, then the difference between A and B denoted A-B is the matrix defined by subtracting the corresponding entries in B from A.
A scalar may be added to a matrix of any dimension.  When add/subtract a scalar to an array, MATLAB adds / subtracts the scalar to every element of the array. If A is a matrix, the expression A+n is evaluated by adding n to each element of A.

Examples:

 >> A = [5 1; 0 9];
>> B = [2 –2; 1 1];
>> A + B
ans =
     7    –1
     1    10
>> A – B
ans =
     3     3
    –1     8 

Matrix Multiplication

The multiplication operation * is executed by MATLAB according to the rules of linear algebra. This means that if A and B are two matrices, the operation A*B can be carried out only if the number of columns in matrix A is equal to the number of rows in matrix B. Such matrices are said to be conformable. Remember that the matrix multiplication is not commutative, i.e., A*B≠B*A (order of multiplication is important). A matrix of any dimension can be multiplied with a scalar. Each element in the matrix is multiplied by the scalar.
There are two ways of multiplying matrices – matrix multiplication and element wise multiplication. For element wise multiplication both matrices must be of same dimension.

Examples

>> A = [1 2; 3 4]
>> B = [0 1/2; 1 -1/2];
>> C = [1 0];

Matrix Multiplication
>> A*B
ans =
     0    16
     2    36
>> B*A
ans =
    14    20
    17    22
>> A*C
??? Error using ==> mtimes
Inner matrix dimensions must agree.

Element wise Multiplication
>> D = A .* B
D =
   0  1
   3 -2
>> A .* C
??? Error using ==> times
Matrix dimensions must agree.

Multiplication with scalar
>> A = [–2 2; 4 1];
>> C = 2*A
C =
    –4     4
     8     2 

Multiplication of two vectors

For multiplication of vectors, they must both be of the same size. One must be a row vector and the other a column vector.
If the row vector is on the left, the product is a scalar. If the row vector is on the right, the product is a square matrix whose side is the same size as the vectors

Example:

>> h = [ 2 4 6 ]
h =
     2     4     6
>> v = [ -1 0 1 ]'
v =
    -1
     0
     1
>> h * v
ans =
     4
>> v * h
ans =
    -2    -4    -6
     0     0     0
     2     4     6

dot product and cross product of vectors

Consider two vectors, X=[x1 x2] and Y = [y1 y2]. The inner product or dot product is defined as X.Y= x1* y1 +x2*y2 = |X|*|Y|*cos( ø). Another important operation involving vectors is the cross product. It is defined as XxY= |X|*|Y|*sin( ø).  In MATLAB, the dot product of two vectors A, B can be calculated using the dot(A,B) command. Cross(A,B) is used to compute the cross product.
>> a = [1;4;7]; b = [2;–1;5];
>> c = dot(a,b)
c =
    33
The dot product can be used to calculate the magnitude of a vector. All that needs to be done is to pass the same vector to both arguments. Consider the vector:
>> J = [0; 3; 4];
Calling dot we obtain:
>> dot(J,J)
ans =
    25
>> A = [1 2 3]; B = [2 3 4];
>> C = cross(A,B)
C =
    –1     2    –1

Matrix Division

The division operation is also associated with the rules of linear algebra. There are two types of division in Matlab - Right division and Left division. Right division performs the normal algebraic division of matrices. It can be used for solving the linear equations of the form XA=B. The solution of the equation is given by X=B/A.
Left division, \, is one of MATLAB's two kinds of array division. This type of division is used to solve the matrix equation AX=B,  A is a square matrix, X and B are column vectors. Solution of the system of equation is given by X = A \ B (Equivalent to inv(A)*B).
Left division is
• 2-3 times faster
• Often produces smaller error than inv()
• Sometimes inv()can produce erroneous results

Example

>> A = [1 2; 3 4];
>> B = [0 1/2; 1 -1/2];
>> A/B
ans =
     5     1
    11     3
>> A\B
ans =
    1.0000   -1.5000
   -0.5000    1.0000
>> A./B
ans =
   Inf     4
     3    -8
>> A.\B
ans =
         0         0.2500
    0.3333   -0.1250

Vector division
>> A = [2 4 6 8]; B = [2 2 3 1];
>> A/B
ans =
    2.1111
>> C = A./B
C =
     1     2     2     8
>> C = A.\B
C =
    1.0000    0.5000    0.5000    0.1250

Power of a Matrix

^ Operator is used to find the power of a matrix. The element wise operation also can be performed.
>> B = [2 4; –1 6]
B =
     2     4
    –1     6
>> B^2
ans =
     0    32
    -8    32
>> B.^2
ans =
     4    16

     1    36
submit to reddit