Download presentation
Presentation is loading. Please wait.
Published byKarsten Hanssen Modified over 5 years ago
1
EECS 1541 -- Introduction to Computing for the Physical Sciences
Week 2: Vectors and Matrices (Part I) READING: – 2.1.2
2
EECS 1541 -- Introduction to Computing for the Physical Sciences
Arrays Vectors and matrices are used to store set of values. Like an array, vectors and matrices can be visualized as a table of values. The dimensions of an array are r by c, where r is the number of rows and c is the number of columns.
3
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Scalars A scalar in MATLAB is an array of size 1 x 1 Example: a variable that stores a single value is an example of a scalar >> z = 6 z = 6
4
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Vectors A vector in MATLAB is equivalent to a 1-dimensional array where one of the dimensions is 1 There are: row vectors and column vectors row vector column vector
5
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors A row vector in MATLAB is an 1 x n array Example of an 1 x 4 array that consists of 4 elements: >> v = [ ] v = >> v = [1, 2, 3, 4] v =
6
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors MATLAB workspace after a row vector has been assigned:
7
Row Vectors: Colon Operator
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator The colon operator can be used to iterate values in a vector Format: Vector_variable_name = initial value : step: final value step size Example 1: >> v = 1:2:7 v =
8
Row Vectors: Colon Operator
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator Example 2: If “step” is omitted, the default step size is 1 >> v = 1:6 v =
9
Row Vectors: Colon Operator
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator Example 3: >> v = 1:3:12 v = Adding 3 to 10 would go beyond 12, so the vector stops at 10
10
Row Vectors: Colon Operator
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator Example 4: >> start = 20; >> step = -3; >> final = 6; v = start:step:final v = Observe that the final value is not guaranteed to be at the end of the vector
11
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Row Vectors: Linspace linspace(x,y,n) function creates a vector with n values in the inclusive range from x to y. It creates a vector with n values that are linearly spaced. If n is omitted, the default is 100 points If n = 1, linspace(x,y,1) returns y Example : >> v = linspace(1,5,3) v =
12
Concatenating the vectors
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Concatenating the vectors Vectors variables can be created using existing variables Example : >> v1 = 2:5; >> v2 = 1:3; >> v_total = [v1 v2] v_total = From v1 From v2
13
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Column Vectors A column vector in MATLAB is a n x 1 array A column vector can be created directly by entering the values of the vector inside a pair of square brackets with the values separated by semi-colons Example : >> v = [2; 4; 6; 8] v = 2 4 6 8
14
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Column Vectors Any row vector created using any method can be transposed to result in a column vector Example : >> r_v = 2:5; >> c_v = r_v’ c_v = 2 3 4 5
15
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Column Vectors A column vector can also be created using the colon notation like the following example: Example : >> v = 1:5; >> c = v(:) c = 1 2 3 4 5
16
Number of elements in a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Number of elements in a vector The function length will return the number of elements in a vector (i.e. both row vectors and column vectors) Example : >> v = 2:3:9 v = >> length(v) ans = 3
17
Number of elements in a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Number of elements in a vector Example : >> v = 3:7; >> w = v’’; >> length(w) ans = 5
18
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector The elements in a vector are numbered sequentially; like an array, each element number is called the index In MATLAB, the indices start at 1 index element To access a particular element in a vector, enter the name of the vector variable and the index in parentheses vector_name()
19
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = >> v(2) ans = index is 2, so get the value of the second element in v -8
20
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = >> v(5) ans = -2 index is 5, so get the value of the fifth element in v
21
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = >> v(end) ans = “end” refers to the last element in the vector
22
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = >> v(end-1) ans = -2
23
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = >> v(2:4) ans = You can return a range of elements by inputting the indices
24
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = >> v([1 3]) ans = The index can be a vector of indices In this example, we want to obtain the first and the third element of the vector
25
Indexing elements of a vector
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> a = linspace(2,6,5); >> b = a’; >> b(length(b)-3) ans = 3 Based on this example, can you think of a general formula on how MATLAB compute all the values in linspace (when n is not equal to 1)?
26
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Magnitude of a vector Magnitude of a vector (v) in n-dimensional can be computed by: Magnitude of a vector can be computed using the norm function For example, the magnitude of the vector (1,1,1) can be computer in MATLAB: >> v = [1 1 1]; >> v_mag = norm(v) v_mag = 1.7321
27
EECS 1541 -- Introduction to Computing for the Physical Sciences
returns an n-by-n matrix of pseudorandom normal values EECS Introduction to Computing for the Physical Sciences Magnitude of a vector You can select the elements in a vector and find out the corresponding magnitude using the norm function >> v = [1 1 1]; Example, >> v = [1 1 1]; >> v_mag = norm(v(1:2)) v_mag = 1.4142
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.