% Jake Bobowski % July 27, 2017 % Created using MATLAB R2014a % This tutorial will show you how to do basic matrix manipulations. This % is where MATLAB really shines. MATLAB, after all, stands for Matrix % Laboratory. clearvars % Here's how you enter, for example, a 3x3 matrix. Elements along the row % are separated by comma. The end of a row is indicated using a semicolon. m = [1, 3, 0, 4; 2, -2, 1, 2; -4, 1, -1, -7] % We can get the size of the matrix. size(m) % Size has a two-component output. Here's a trick we can use to get the % number of rows assigned to a and the number of columns assigned to b. [a b] = size(m) % We can now index m to select particular rows or columns or elements. % Here's the element in the 2nd row, 3rd column. m(2, 3) % Here's entire 2nd row. m(2, :) % Here's the entire 3rd column. m(:, 3) % I can assign a new value to a specific element. m(2, 2) = 99; m % I can also assign new values to an entire row... m(2, :) = (10:10:40); m % ... or column. m(:, 3) = (-10:-10:-30); m % m is a 3x4 matrix. Let's define a 4x3 matrix n = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12] % Now we can multiply our 3x4 matrix and or 4x3 matrix to create a 3x3 % matrix... m*n % ...or a 4x4 matrix. n*m % Just a word of caution. You can also do element-by-element % multiplication of two of matrices by placing a dot in front of *, as % in .*, see the example below: x = [1:3; 4:6] y = [10:10:30; 40:10:60] x.*y % Let's create a square matrix so that we can take its inverse. MATLAB % will automatically create 'magic nxn matix' using magic(n). In a magic % matrix all the rows, columns, and diagonals add up to the same number. m3 = magic(3) % Here's the inverse of m4. im3 = inv(m3) % We can check that the matrix product of m3 and im3 produces the identity % matrix. m3*im3 % It's also easy to take the transpose of matrices. Of course, the % transpose of a magic matrix is still magic! Notice the dot (.) in front % of ', without the dot MATLAB would transpose the matrix and then take the % complex conjugate of each element. It doesn't matter for a matrix in % which all of the elements are real, but it would be important if you had % some complex-values elements. See below when we discuss the Hermitian % Conjugate. m.' m3.' % Someone in a qunatum mechanics course may want to take the Hertitian % conjugate of a matrix. % First, let's create a complex-valued matrix. mc = [i, 3*i, 4; 2, -2, -2*i] % The Hermitian Conjugate can be calcuated using... ctranspose(mc) % ...or simply by: mc' % Notice that, as stated above, the ' operator transposes and conjugates % the matrix.
m = 1 3 0 4 2 -2 1 2 -4 1 -1 -7 ans = 3 4 a = 3 b = 4 ans = 1 ans = 2 -2 1 2 ans = 0 1 -1 m = 1 3 0 4 2 99 1 2 -4 1 -1 -7 m = 1 3 0 4 10 20 30 40 -4 1 -1 -7 m = 1 3 -10 4 10 20 -20 40 -4 1 -30 -7 n = 1 2 3 4 5 6 7 8 9 10 11 12 ans = -17 -19 -21 350 400 450 -280 -320 -360 ans = 9 46 -140 63 30 118 -320 174 51 190 -500 285 72 262 -680 396 x = 1 2 3 4 5 6 y = 10 20 30 40 50 60 ans = 10 40 90 160 250 360 m3 = 8 1 6 3 5 7 4 9 2 im3 = 0.1472 -0.1444 0.0639 -0.0611 0.0222 0.1056 -0.0194 0.1889 -0.1028 ans = 1.0000 0 -0.0000 -0.0000 1.0000 0 0.0000 0 1.0000 ans = 1 10 -4 3 20 1 -10 -20 -30 4 40 -7 ans = 8 3 4 1 5 9 6 7 2 mc = 0.0000 + 1.0000i 0.0000 + 3.0000i 4.0000 + 0.0000i 2.0000 + 0.0000i -2.0000 + 0.0000i 0.0000 - 2.0000i ans = 0.0000 - 1.0000i 2.0000 + 0.0000i 0.0000 - 3.0000i -2.0000 + 0.0000i 4.0000 + 0.0000i 0.0000 + 2.0000i ans = 0.0000 - 1.0000i 2.0000 + 0.0000i 0.0000 - 3.0000i -2.0000 + 0.0000i 4.0000 + 0.0000i 0.0000 + 2.0000i