%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % A simpler way to set a matrix with only a few nonzero elements A = zeros(5,5); A = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A(1,1) = 3 A = 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A(2,3) = 5 A = 3 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 % In a script file you would put these all together like so A = zeros(5,5); A(1,1) = 3; A(2,3) = 5; % etc. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % The colon operator: a fundamental trick in matlab % 1:5 means make a vector of the numbers from 1 to 5 1:5 ans = 1 2 3 4 5 % exactly the same result as x = [1 2 3 4 5] x = 1 2 3 4 5 % vector of numbers from 21 to 31 x = 21:31 x = 21 22 23 24 25 26 27 28 29 30 31 % vector of numbers from 21 to 31, in steps of 2 x = 21:2:31 x = 21 23 25 27 29 31 % vector of numbers from 0 to 1, in steps of 0.1 x = 0:0.1:1 % 0 to 1 in steps of 0.1 x = Columns 1 through 8 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 Columns 9 through 11 0.8000 0.9000 1.0000 load A.asc A A = 0 1 2 3 7 8 0 0 7 8 1 9 7 3 4 0 % Access an individual element of a matrix A(2,2) ans = 8 % Accessing a range of elements of matrix, here the 2 & 3 rows in the 2nd column A(2:3,2) ans = 8 8 % Accessing a range of elements of matrix, here the 2nd-4th rows in the 2nd column A(2:4,2) ans = 8 8 3 % Accessing a range of elements of matrix, here the 2nd-4th rows in the 1st-2nd columns A(2:4,1:2) ans = 7 8 7 8 7 3 % Next: colon by itself means ALL rows or ALL columns % all rows of A in 1st columns A(:,1) ans = 0 7 7 7 % all rows of A in 2nd columns A(:,2) ans = 1 8 8 3 A A = 0 1 2 3 7 8 0 0 7 8 1 9 7 3 4 0 % 1st row, all columns A(1,:) ans = 0 1 2 3 % 4th row, all columns A(4,:) ans = 7 3 4 0 % Transpose % set x to a row vector x = A(4,:) x = 7 3 4 0 % transpose operator turns rows into columns, columns into rows % transpose of row vector x is a column vector x' ans = 7 3 4 0 % One use: rathe than entering right-hand-side vetor b like this b = [4 ; 3 ; 7.2 ; 9] b = 4.0000 3.0000 7.2000 9.0000 % enter it as a row vector then take transpose b = [4 3 7.2 9]' b = 4.0000 3.0000 7.2000 9.0000 % observe what transpose does to a matrix A A = 0 1 2 3 7 8 0 0 7 8 1 9 7 3 4 0 A' ans = 0 7 7 7 1 8 8 3 2 0 1 4 3 0 9 0 % A couple a utility functions A = zeros(4,4) A = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A = ones(4,4) A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 A = eye(4,4) A = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 % "eye" is another matlab pun: "eye" mean I, the identity matrix I = eye(4,4) I = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 % Observe what multiplication by identity matrix does to a vector x = rand(4,1) x = 0.6324 0.0975 0.2785 0.5469 I*x ans = 0.6324 0.0975 0.2785 0.5469 % Note I*x = x % Random matrix A = rand(4,4) A = 0.9575 0.9572 0.4218 0.6557 0.9649 0.4854 0.9157 0.0357 0.1576 0.8003 0.7922 0.8491 0.9706 0.1419 0.9595 0.9340 % Null matrix i.e. a 0 x 0 matrix A = [] A = %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plotting x = 0:0.01:2*pi; plot(x,sin(x)) xlabel('x') ylabel('sin(x)') clf() plot(x,sin(x),'b', x,cos(x),'r') legend('sin(x)','cos(x)') xlabel('x') ylabel('f(x)') clf() plot(x,sin(x),'b-.', x,cos(x),'r--') clf() x = 0:0.1:10 clf(); semilogy(x,exp(-x),'b-.') clf(); semilogy(x,x.^3),'b-.') | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Elementwise arithmetic x = 1:5 x = 1 2 3 4 5 x = (1:5)*2 x = 2 4 6 8 10 x = (1:5) + 2 x = 3 4 5 6 7 y = 10:14 y = 10 11 12 13 14 x + y ans = 13 15 17 19 21 % however, some operations must be specified as elementwise using "dot" syntax x x = 3 4 5 6 7 x.^2 % .^ means apply the power to each element ans = 9 16 25 36 49 % Note this doesn't work x^2 Error using 'mpower' Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead.} plot(x,x.^2)