Você está na página 1de 9

GENN004: Introduction to Computers

09-Feb-13

Matrices
Vectors and Matrices

Outline
Vectors and Matrics Array Initialization Subarrays Loops with arrays/matrices

Matrices

GENN004: Introduction to Computers

09-Feb-13

Scalar vs Array
Scalar: indicates a variable that holds only a single value at a time.
This is compared to an array, which holds many values at once

Array: is an indexed grouping of values, addressed with subscripts


Array A = (10, 20, 30, 40, 50) A(1)=10 A(2)=20 A(3)=30

Vectors and Matrices


A Matrix is defined as an array with two or more dimensions Vectors are defined as arrays with either one row and many columns, or one column and many rows
A row vector has many columns e.g. it is a single row A column vector has many rows e.g. it is a single column

Matrices

GENN004: Introduction to Computers

09-Feb-13

Array initialization
B = [1.0 2.0 3.0] creates a 1 x 3 array
Is this a column or row vector?

C = [1.0; 2.0; 3.0] creates a 3 x 1 array


The semicolon ; tells MATLAB to start a new row in the array You can also separate rows by pressing enter and continuing on the next line C= [1.0 2.0 3.0]

Array initialization
D = [1, 2, 3; 6, 5, 4] creates a 2 x 3 array
The commas are optional

E = [ ] creates a special empty array having no rows and no columns F = [ 1, 5, 6; 5, 4 ] would create ...?
An error message! All rows must have the same size, or the array is not valid

G = [ 1 2 3 ]; H = [ 4 5 6 ]; I = [G; H]; would create ...?

Matrices

GENN004: Introduction to Computers

09-Feb-13

Array initialization
MATLAB includes a very useful colon operator
Makes creating sequences very simple

The syntax is
first:increment:last

first is the first value in the series increment is how big each gap between values should be increment can be skipped last is the final value in the series my_colon = 1:2:10 my_colon = [ 1 3 5 7 9 ]

Array initialization functions


The transpose operator swaps rows and columns of arrays it is applied to X = [ 1 2 3 ]; 1 Y = X; Y = 2
3

Can be combined to create more complex results Z = [ X X ] Z= 2 2


3 3 1 1

Matrices

GENN004: Introduction to Computers

09-Feb-13

Array initialization functions


zeros(n) zeros(m,n) zeros(size(arr)) ones(n) ones(m,n) ones(size(arr)) eye(n) eye(m,n) length(arr) n x n matrix of zeros m x n matrix of zeros matrix of zeros the size of arr n x n matrix of ones m x n matrix of ones matrix of ones the size of arr n x n identity matrix m x n identity matrix Returns length of vector or longest dimension of array Returns two values: number of rows and number of columns

size(arr)

Referring to and modifying elements


The elements are numbered sequentially (starting from 1); each element number is called the index, or subscript. Examples: >> a=1:2:9 Index a(4)=?, a(2)=? Element a(0)=?, a(6)=? >> mat = [2:4; 3:5] mat = 234 345 mat(2,3) = ? mat(3,2) = ?

Matrices

GENN004: Introduction to Computers

09-Feb-13

Subarrays
MATLAB allows special subarray use array = [1.1 -2.2 3.3 -4.4 5.5];
array(3) is 3.3 array([1 4]) is itself an array, [1.1 -4.4], or the first and fourth elements in array array(1:2:5) is array [1.1 3.3 5.5] or the odd elements in array

A special function end can be used to create array subscripts


array(end) would be 5.5 array(3:end) would be [3.3 -4.4 5.5]

Subarrays
MATLAB allows advanced subarray indexing on the left side of an assignment
array =
1 2 3 4 5 6 7 8 9 10 11 12 The shape of the left hand side subarray index

array(1:2, [1 4]) 20 = [20 21; 22 23] 2 3 21


array =
22 6 7 23 9 10 11 12

must match the right side array!

Matrices

GENN004: Introduction to Computers

09-Feb-13

Test it out!
Try the following statements out in MATLAB and see what happens:
array_1 = [ 1:.2:2; 2:.2:3] array_2 = array_1(1,2:end) array_1(1, end) = 4 Change elements (1,1), (1,2), (2,1), (2,2) in one statement Change elements (1,1), (2,1), (1,5), (2,5) in one statement

Loops with Vectors


In most programming languages when performing an operation on a vector, a for loop is used to loop through the entire vector, using the loop variable as the index into the vector. for i = 1:length(vec) % do something with vec(i) end % Example v=[3 7 2 1] for i = 1:length(v) v(i) =v(i) * 3; End % v=????

Matrices

GENN004: Introduction to Computers

09-Feb-13

Loops with Matrices


Similarly, for an operation on a matrix, a nested loop would be required, and the loop variables over the rows and columns are used as the subscripts into the matrix. [r c] = size(mat); for row = 1:r for col = 1:c % do something with mat(row,col) end end

Getting Maximum and Minimum Value


v=[1 3 2 7 4 2] max=0; for =1:length(v) if v(i)>max max=v(i); maxi=i; end end % Repeat for the minimum % Repeat for a matrix i
1 2 3 4 5 6

v(i)
1 3 2 7 4 2

max

maxi

0 1 ? 1 1 3 1 2 3 2 3 7 2 4 7 4 7 4

Matrices

GENN004: Introduction to Computers

09-Feb-13

Counting Elements
v=[1 3 2 -7 4 -2] c=0; for =1:length(v) if v(i)>0 c=c+1; end end % Repeat for the negative % Repeat for a matrix i
1 2 3 4 5 6

v(i)
1 3 2 -7 4 -2

c
0 1 2 3 3 4 1 2 3 4

Comparing Elements
v1=[1 3 2 7 4 -2] v2=[5 3 4 1 2 -2] for i=1:length(v1) if v1(i)>v2(i) v(i)=1; elseif v1(i)<v2(i) v(i)=-1 else v(i)=0; end end i
1 2 3 4 5 6

v1(i)
1 3 2 7 4 -2

v2(i)
5 3 4 1 2 -2

v(i)
-1 0 -1 1 1 0

Matrices

Você também pode gostar