0% found this document useful (0 votes)
106 views7 pages

Matrices in Matlab: Topic 6 Arrays and Vectors

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. You can extract individual values from an array by specifying the index within the array using round brackets.

Uploaded by

spotanand9941
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views7 pages

Matrices in Matlab: Topic 6 Arrays and Vectors

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. You can extract individual values from an array by specifying the index within the array using round brackets.

Uploaded by

spotanand9941
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Topic 6

Arrays and Vectors


An array is a collection of data objects of the same type, typically stored sequentially in memory.

Matrices in Matlab

An array is an elementary data structure. Almost all programming languages provide support for arrays. Matlab is a language that has been particularly specialised 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.

1 2.5 5

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

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

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

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

10

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

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 = c = 2 b(1) % c is set to the value of the first element of b

Size of an Array
Matlab keeps track of the size of arrays and ensures you do not try to go beyond their bounds. For example:
>> b(4) ??? Index exceeds matrix dimensions. >> b(0) ! ??? Index into matrix is negative or zero.

You can also assign new values to individual elements of an array. For example:
>> b(3) = 6 b = 2 5 % Set the value of the 3rd element of b to 6 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.

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

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] ! a = 1 4 2 5 3 6 % The ; separates the % individual 1D arrays.

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 4 5 6 ]; % A matrix consisting of two rows

As far as Matlab is concerned, everything is a matrix! A vector is a 1xN (or Nx1) matrix; a scalar is a 1x1 matrix.

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Matrix and vector operators


The standard mathematical operators can be applied to vectors and matrices. Matlab handles all the details automatically. For example, suppose we have two matrices defined as: >> a = [1 2 3 4]; >> b = [5 6 7 8]; The transpose operator switches the rows and columns of a matrix. The transpose operator is denoted with the single apostrophe (') symbol. >> a' ans = 1 2 3 4 % The transpose rows and % columns. % Initialise two matrices.

Addition and subtraction


Matrix addition and subtraction is same as linear algebra. For example:
>> c = a + b c = 6 10 8 12 % Addition of a scalar results % in the scalar being added to % the matrix elements. % Matrix addition (and subtraction).

>> c = a + 2 c = 3 5 4 6

For matrix addition or subtraction to work, the dimensions of the two matrices must match.

Transpose has higher precedence than multiplication.

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Matrix multiplication
Matrix multiplication is defined as in standard linear algebra. For example:
>> c = a * b c = 19 22 43 50 >> c = a * 2 % Matrix multiplication.

Point-wise Multiplication
Associated with matrices and vectors are a number of special operators, many of which are unique to Matlab. The .* operator performs point-wise multiplication on each corresponding pair of elements of two matrices (sometimes called array multiplication). For example:
>> c = a .* b % Point-wise multiplication. c = 5 21 12 32

% % % %

Matlab scalar Matlab scalar

recognises this as multiplication. also recognises division.

c = 2 6 4 8

For matrix multiplication to work, the number of columns in the first matrix must match the number of rows in the second matrix.

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Point-wise Division and Exponentiation


The ./ operator performs point-wise division on each corresponding pair of elements of two matrices. For example:
>> c = a ./ b c = 0.2000 0.4286 0.3333 0.5000 % Point-wise division.

Matrix Division
Matrix division implies solving for matrix inverses. Matlab handles this automatically! Note that because matrix multiplication is not commutative, we require the concept of left and right division. Right division is post-multiplication by the inverse of a matrix: >> c = a / b; c = 3 2 -2 -1 % c = a * b-1

The .^ operator performs point-wise exponentiation on each corresponding pair of elements of two matrices. For example:
>> c = a .^ b c = 1 2187 64 65536 % Point-wise exponentiation.

Left division is pre-multiplication by the inverse of a matrix: >> c = a \ b c = -3 4 -4 5 % c = a-1 * b

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Checking...
Left division is the most common. The expression c = a\b above would solve the equation b = a*c. Double check:
>> a*c ans = 5 7 6 8

Array constructors - the colon operator


It is easy to construct small arrays by explicitly specifying all the elements, but this is not practical for large arrays. Matlab provides the colon operator (:) for constructing sequences of values. The colon operator produces an array equivalent to the elements of an arithmetic sequence. Arithmetic sequences are defined in terms of the first value in the series, the increment between successive values, and the last value in the series. The syntax for building an array using the colon operator is: array = first : increment : last

which is the value of the b matrix. Most of these expressions would require at least 5 lines of code if programmed in some other language such as C or Java. Matlab's syntax yields very concise and readable code.

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Colon Operator
For example:
>> x = 3 : 2 : 11 x = 3 5 7 9 11

Subarrays
As well as selecting individual elements from arrays, Matlab allows for the selection of sections of an array. For example:
>> a = [10:-1:1] a = 10 >> a(4:9) 9 8 7 6 5 4 3 2 1

If the increment is 1, it can be omitted. For example:


>> x = 1 : 10 x = 1 2 3 4 5 6 7 8 9 10

The colon operator is enormously useful, not only for array creation but also for loop control.

% Use the colon operator (with a % default increment of 1) to select % elements 4 to 9 from the array. 6 5 4 3 2

ans = 7

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Subarrays (cont.)
You can also assign values to subarrays.
>> a(1:3) = [8 9 10] a = 8 9 10 7 6 5 4 3 2 1

Extracting Data from 2D Arrays


Matrix selection operations are extended to 2D arrays in a natural way.
>> a = [1 2 3 4 5 6 7 8 9] % Define a 2D array.

>> a(2, 3) ans = 6 >> a(2,1:3) ans = 4

% Get the value at row 2, column 3.

The size of the array being assigned must match the size of the array selected.

% Get row 2, columns 1 to 3.

6 % Get the values from rows % 2-3 in columns 1-2.

>> a(2:3, 1:2) ans = 4 7

5 8

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Extracting all rows or columns


If you want to extract all the rows (or all the columns) from a matrix, you can use the empty colon operator to specify the row or column. For example:
>> a(:, 2) ans = 2 5 8 >> a(2:3, :) ans = 4 7 5 8 6 9 % Get the values from rows % 2-3 in all columns. % Get the values from every % row in column 2. % Same as a(1:3, 2)

Matrix concatenation
Matlab has a very convenient syntax for concatenating matrices - just stick the matrices side by side, or on top of each other, within a set of enclosing square brackets.
>> a = [1 2 3] >> b = [a 7 8] b = 1 2 3 % % % % % 7 8 % Concatenate 7 and 8 onto the % end of a.

>> a = [a a(1:2) b ]

Construct a new matrix a. The first row is the "old a" with elements 1:2 of a concatenated to the end. The second row is made from array b.

a =

1 1

2 2

3 3

1 7

2 8
22

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Assigning to arrays and subarrays


Note that in the last example, the memory required to store the new matrix will not "fit into" the old space occupied by the original matrix. Matlab will handle any memory allocation needed to make matrices fit. Matlab handles assignment to subarrays and arrays differently. For assignment statements involving subarrays, the shapes of the subarrays on either side of the equal sign must match. Otherwise, Matlab will produce an error. The assignment will only replace the specified elements. In contrast, assigning to an existing array will replace the entire contents of the array, may even resize the array.

Summary of Arithmetic Operations

24

Matlab

Computing for Engineers and Scientists CITS1005

Matlab

Computing for Engineers and Scientists CITS1005

Arithmetic Operations between Two Scalars


Operation Addition Subtraction Multiplication Division Exponentiation Algebraic Form Matlab Form a+b a-b a*b a/b a^b

Common Array and Matrix Operations (1)


Operation Addition Subtraction Matlab Form a+b a-b Comments Array addition and Matrix addition are identical Array subtraction and matrix subtraction are identical Element-by-element multiplication of a and b. Both arrays must of the same shape, or one of them must be a scalar Matrix multiplication of a and b. The number of columns in a must be equal to number of rows in b.

Array Multiplication

a .* b

Matrix Multiplication

a*b

26

Matlab

Computing for Engineers and Scientists (CITS1005)

Matlab

Computing for Engineers and Scientists (CITS1005)

Common Array and Matrix Operations (2)


Operation Matlab Form a ./ b Comments Element-by-element division of a and b: a(i,j)/b(i,j). Both arrays must of the same shape, or one of them must be a scalar. Element-by-element division of a and b, but b in the numerator b(i,j)/a(i,j). Both arrays must of the same shape, or one of them must be a scalar.

Common Array and Matrix Operations (1)


Operation Matrix Right Division Matrix Left Division Matlab Form a/b Comments Matrix division is defined by a*inv(b), where inv(b) is the inverse of matrix b. Matrix division is defined by inv(a)*b, where inv(a) is the inverse of matrix a. Element-by-element exponentiation of a and b: a(i,j)^b(i,j). Both arrays must of the same shape, or one of them must be a scalar.

Array Right Division

a\b

Array Left Division

a .\ b

Array Exponentiation

a .^ b

You might also like