0% found this document useful (0 votes)
84 views8 pages

3 - 2 Matrix Operation

This document provides an introduction to arrays and matrices in MATLAB. It defines arrays and vectors, and how to declare and perform operations on arrays such as addition and accessing elements. Matrices are defined as 2D arrays, and how to declare and multiply matrices is described. Examples are provided for adding, subtracting, multiplying matrices by a constant, and transposing matrices. The conclusion states that everything in MATLAB can be considered a matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views8 pages

3 - 2 Matrix Operation

This document provides an introduction to arrays and matrices in MATLAB. It defines arrays and vectors, and how to declare and perform operations on arrays such as addition and accessing elements. Matrices are defined as 2D arrays, and how to declare and multiply matrices is described. Examples are provided for adding, subtracting, multiplying matrices by a constant, and transposing matrices. The conclusion states that everything in MATLAB can be considered a matrix.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NCSS Lab Manual Study of Basic Array & Matrix Operations

Title: Study of Basic Array & Matrix Operations


Aim: Study of Basic Array & Matrix Operations
Objectives:
● Familiarization with technical computing software MATLAB

Theory:
Arrays and Vectors
An array is a collection of data objects of the same type, typically stored sequentially in memory.
• An array is an elementary data structure.
• Almost all programming languages provide support for arrays.
• MATLAB is a language that has been particularly specialized to support arrays (and subsequently
matrices).
• An array is the obvious way to represent a vector.
• A 3D vector with coordinates [1, 2.5, 5] would be represented as an array of 3 doubles arranged
sequentially in memory.
Declaring (Constructing) Arrays
• In MATLAB, we can construct an array and associate it with an identifier very
easily. For example: >> a = [1, 2.5, 5] a = 1.0000 2.5000 5.0000
• The commas are optional and can be omitted:
>> a = [1 2.5 5] a = 1.0000 2.5000 5.0000
• The square brackets indicate to MATLAB that the contents represent an array:
>> whos Name Size Bytes Class a 1x3 24 double array Grand total is 3 elements using 24 bytes

Operations on Arrays
• This process of constructing an array involves a segment of memory being allocated and
associated with the variable name, and the elements of memory being set to the specified values.
• In most programming languages you would have to declare an array and assign the values one at a
time. In MATLAB this is automatic.
• You can then perform arithmetic on arrays as simply as you can with scalars. For example:
>> b = a*2 b = 2 5 10

Working with Array Elements


• You can extract individual values from an array by specifying the index within the array using
round brackets. For example:
>>c = b(1) % c is set to the value of the first element of b c = 2
• You can also assign new values to individual elements of an array. For example:
>> b(3) = 6 % Set the value of the 3rd element of b to 6 b = 2 5 6
• In MATLAB, the index of the first element of an array is always 1.
• Note: this differs from languages such a C or Java where the index of the first element is always 0.

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


1
NCSS Lab Manual Bisection Method

Matrices
• An array is a collection of data objects of the same type.
• The data objects of the array can themselves be arrays.
• A matrix is typically represented by an array of arrays, or a 2D array.
MATLAB supports matrices in the same way that it supports vectors.
• MATLAB uses the semi-colon (;) operator to distinguish between the different
rows of a matrix. For example:

>> a = [1 2 3; 4 5 6] % The ; separates the


% individual 1D arrays.

a=
1 2 3
4 5 6

Everything in MATLAB is a Matrix


• MATLAB also allows rows to be entered on different lines.
• Once an array is started by a square bracket ([), MATLAB assumes that a new
line means a new row of the matrix. For example:
>> a = [1 2 3 % A matrix consisting of two rows 4 5 6 ];
• As far as MATLAB is concerned, everything is a matrix!
• A vector is a 1xN (or Nx1) matrix; a scalar is a 1x1 matrix.
Matrix and vector operators:
Matrices are often used in algebra to solve for unknown values in linear equations, and in
geometry when solving for vectors and vector operations.

Example 1)
- There are 2 rows and 3 columns in matrix M. M would be called a 2 x 3 (i.e. “2 by 3”)
matrix.

PART A - Matrix Addition

We can add matrices together as long as their dimensions are the same, i.e. both matrices
have the same number of rows and columns. To add two matrices, we add the numbers of
each matrix that are in the same element position.
Example 2:

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


5
NCSS Lab Manual Bisection Method

PART B - Matrix Subtraction


We can subtract matrices in a similar way to addition. Both matrices need to have the same
dimensions, and we subtract the numbers of the second matrix from the first that are in the
same element position.
We can subtract matrices in a similar way to addition. Both matrices need to have the same
dimensions, and we subtract the numbers of the second matrix from the first that are in the
same element position.
Example 3)

PART C - Multiplying a Matrix by a Constant

We can multiply a matrix by some value by multiplying each element with that value. The
value can be positive or negative

We can get the negative of a matrix by using the above multiplication method:

PART D - Multiplying Matrices

We can multiply a matrix (A) by another matrix (B) if the number of columns in A is equal to
the number of rows in B (in bold). Multiplication of A by B is typically written as A(B) or
(A)B.

- A has 2 rows and 3 columns and B has 3 rows and 1 column so we can multiply A by B.
Notice that we can’t multiply B by A in this particular case because the number of columns
in B is not equal to the number of rows in A (in bold).

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


5
NCSS Lab Manual Bisection Method

- This means that multiplying matrices is not commutative: A(B) ≠ B(A) To multiply
matrices, there’s a convention that is followed.
Let

Let’s look at the first row of A and the first column of B. Element a is multiplied by element
e. Element b is multiplied by element g. The value of the element in the first row and first
column of A(B) is the sum of the products (ae + bg).

Notice A(B) is now a .2 x 1. matrix.

PART E - Transposing a Matrix

To transpose a matrix, we swap the rows for the columns. To indicate that we are transposing
a matrix, we add a “T” to the top right-hand corner of the matrix.

Exercise:

Given the following matrices, please solve the questions below and if you can’t solve the
problem, explain why:

1) A + F 2) E – D 3) C + B 4) C(D) 5) A(F) 6) CT 7) FT(E)

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


5
NCSS Lab Manual Bisection Method

Solution-:
1) >> A=[3 1 5;6 2 0]

A=

3 1 5
6 2 0

>> F=[2 1 3;5 7 -2]

F=

2 1 3
5 7 -2

>> A+F

ans =

5 2 8
11 9 -2

2) >> matrixE=[3 -2;1 4]

matrixE =

3 -2
1 4

>> matrixD=[5 2;3 1]

matrixD =

5 2
3 1

>> matrixE-matrixD

ans =

-2 -4
-2 3
Electrical Engineering Department Sanjivani College of Engg., Kopargaon
5
NCSS Lab Manual Bisection Method

3) >> matrixC=[2 4;3 6;-1 2]

matrixC =

2 4
3 6
-1 2

>> matrixB=[6;4;-1]

matrixB =

6
4
-1

>> matrixC+matrixB

ans =

8 10
7 10
-2 1

4) >> matrixC=[2 4;3 6;-1 2]

matrixC =

2 4
3 6
-1 2

>> matrixD=[5 2;3 1]

matrixD =

5 2
3 1

>> matrixC(matrixD)

ans =

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


5
NCSS Lab Manual Bisection Method

6 3
-1 2

5) >> matrixA=[3 1 5;6 2 0]

matrixA =

3 1 5
6 2 0

>> matrixF=[2 1 3;5 7 -2]

matrixF =

2 1 3
5 7 -2

>> matrixA(matrixF)
Array indices must be positive integers or logical values.

6) >> C=[2 4;3 6;-1 2]

C=

2 4
3 6
-1 2

>> A=C'

A=

2 3 -1
4 6 2

7) >> F=[2 1 3; 5 7 -2]

F=

2 1 3
5 7 -2

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


5
NCSS Lab Manual Bisection Method

>> E=[3 -2;1 4]

E=

3 -2
1 4

>> A=F'

A=

2 5
1 7
3 -2

>> A(E)
Array indices must be positive integers or logical values

Conclusion:

Prepared by, Approved by,


Prof. A. M. Deulkar Dr. D. B. Pardeshi

Electrical Engineering Department Sanjivani College of Engg., Kopargaon


5

You might also like