





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A workshop guide for using matrices (arrays) in MATLAB. It covers creating matrices, matrix properties, transposing arrays, extracting rows and columns, scalar-matrix operations, and matrix algebra. The document also includes exercises for practicing these concepts.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!
Property Comment
var_name
user chooses names
value
all numerical values are
both a and b are double precision if b = 0, MATLAB only displays real part, a memory location MATLAB assigns - you do not need to worry about this
Order of Precedence Symbol Meaning
1 ( )^ group together ' (^) transpose (exchange rows and columns) 2 .^ (^) raise each element to indicated power ^ (^) multiply matrix by itself indicated number of times .* (^) element-by-element multiplication ./ (^) element-by-element right division .\ (^) element-by-element left division
\ (^) matrix left division
Notation Action Result a:c (^) creates vector with elements 1 apart [a a+1 a+2 ... c] a:b:c creates vector with elements b apart (^) [a a+b a+2b ... (≤≤≤≤c)] A(m,:) (^) selects m th^ row of A A(:,j) selects j th^ column of A
A(m:n,:) selects m th^ through nth^ rows of A A(:,j:k) selects j th^ through k th^ columns of A
A(m:n,j:k) selects m th^ through nth^ rows of A and j th^ through k th^ columns of A
number of rows and columns
[rows, cols] = size(x)
» Array2trans = Array2' Array2trans = 3 -4 12 12 -3 5
Array2 has been “transposed”, i.e., the columns and rows were interchanged so that the first column became the first row, etc.
(3) Extracting rows or columns from an array in MATLAB.
Enter the following at the Command Line prompt » row2_Array1 = Array1(2,:) row2_Array1 = 4 -3 6 The second row of Array1 has been extracted using the colon operator.
Enter the following at the Command Line prompt » col1_Array2 = Array2(:,1) col1_Array2 = 3
12 The first column of Array2 has been extracted using the colon operator.
Enter the following at the Command Line prompt » Array1sub = Array1(1:2,2:3) Array1sub = 9 - -3 6 A subset array consisting of elements from the 1st^ and 2nd^ rows, 2nd^ and 3rd^ columns of Array1 has been created.
(4) Determining the size of a matrix.
Enter the following at the Command Line prompt » size(Array1) ans = 3 3 size returns the number rows and columns in an array.
Enter the following at the Command Line prompt » [A2rows, A2cols] = size(Array2) A2rows = 3 A2cols = 2 We can assign the number of rows and columns in a matrix to variables for later use.
Enter the following at the Command Line prompt
» size(row2_Array1) ans = 1 3 The size of a row vector is (1,N) for 1 row, and N elements. What do you think the size of a column vector is? Contrast size with length for a vector.
B = exp(A)
(5) Functions of matrices in MATLAB.
Enter the following at the Command Line prompt » Amat = [1 10; 100 1000] Amat = 1 10 100 1000 » log10_Amat = log10(Amat) log10_Amat = 0 1. 2.0000 3. » » Bmat = [0 pi/6 pi/3; pi/2 2pi/3 5pi/6] Bmat = 0 0.5236 1. 1.5708 2.0944 2. » sin_Bmat = sin(Bmat) sin_Bmat = 0 0.5000 0. 1.0000 0.8660 0.
(6) Scalar-matrix operations in MATLAB.
Enter the following at the Command Line prompt » const = 5; » Cmat = Amat + const Cmat = 6 15 105 1005 The constant value 5 was added to each element of Cmat.
Enter the following at the Command Line prompt
Cmat are displayed as 1.0e+003 *. This means that each number that follows should be multiplied by 10 3 in order to produce the proper element value. MATLAB automatically moves to scientific notation type display when necessary.
Contrast the preceding with » Hmat = Amat*log10_Amat Hmat = 1.0e+003 * 0.0200 0. 2.0000 3. Using the multiplication operator, , instead of the element-by-element multiplication operator, ., instructed MATLAB to follow the rules of matrix multiplication (outlined below) and produces an entirely different answer.
th
th
=
k Q
(8) Matrix algebra in MATLAB.
Define (enter) the following matrices at the MATLAB command prompt.
Try performing the following mathematical operations
What are the results? What error messages are generated?
t
t
t