************************************************************************* A. MATRIX or MATRIXES or MATRICES and MATLAB ************************************************************************* % Data model : Abstraction that describes how data are represented and used MATRIX: ARRAY: internal representation of a matrix [is the abstraction] [is storage inside ] MATLAB abstraction that allows users to manipulate data EVERYTHING in MATLAB is a MATRIX. A Variable is a matrix with one object. An ARRAY is being used to store a matrix An array is a sequence of elements of the same (more often than not) of the same data type MATLAB allows users to view a matrix as matrix (default) but also to access the internal representation of a matrix (aka array) >> a=10; >> whos Name Size Bytes Class Attributes a 1x1 8 double a is 1x1 matrix (aka variable, scalar) and is represented internally by a double array array is a sequence of (sometimes) elementary data types . double is the elementary data type. REVIEW data type : set of values a variable takes Data model : Abstraction that describes how data are represented and used types of data type : primitive (aka built-in) and composite primitive data types in MATLAb, logical,char, single, double, int8, int16, etc, uint8, uint16, etc composite : logical array, double array etc (sort of) and function_handle . More later in the course! ********************************************************* B. MATRICES in MATLAB, matrix and vector, row vector, scalar, square ********************************************************** m x n : generic matrix everything below is a matrix by default m=n n x n : square #rows== # columns m x m m=1 1 x n : row vector n=1 m x 1 : (column) vector [the column can be dropped; by default a vector is a column vector] m=n=1 1 x 1 : scalar (i.e. number, value) ******************************************************* C. COLUMN-MAJOR form/fillin vs ROW-MAJOR form/fillin transpose of a matrix retrieving elements special variable end ARRAY REPRESENTATION OF A MATRIX ******************************************************* Input of matrix in row-major form (also called fill-in) : row 1 then row 2 then row 3 and so on Output (Printout) is in column-major form (fill-in) : column 1, then column 2 and so on A matrix is stored internally as an array in column-major form! The transpose of matrix a is represented in MATLAB by transpose(a) or a.' and is the matrix whose rows are the columns of a (or whose columns are the rows of a). Such for a matrix that is the transpose of another, row and columns get interchanged. If a is mxn then a.' is nxm. If all the elements of a are real numbers we can write a' a.' transpose(a) : all three for a with real numbers give transpose a.' transpose(a) : if elements may not be real numbers (eg complex) use only those two. ************************************************************************ >> a=1; % We will explain later >> [ 1 2 3 4; 5 , 6 , 7 , 8 ; 9 10 11 12]; % space or a comma (,) to separate columns % ; to separate rows % YOU INPUT a matrix in row-major form/fillin ans = 1 2 3 4 5 6 7 8 9 10 11 12 % 3 rows and 4 columns >> whos Name Size Bytes Class a 1x1 8 double ans 3x4 96 double 3x4 is the geometry 3 is number of rows, 4 number of columns x is the multiplication symbol. Give matrix a name >> a=ans; % MATLAB is weakly typed; a ceased to be 1x1 and is a 3x4 now (HERE IS THE EXPLANATION) >> whos >> a' % transpose of a, rows become columns and columns rows ans = 1 5 9 2 6 10 3 7 11 4 8 12 %If a is m x n, then a' is n x m % 4 rows and 3 columns >> whos Name Size Bytes Class a 3x4 96 double << original matrix ans 4x3 96 double << transpose >> a(2,3) % retrieve element of 2nd row and 3 column i.e. 7 ans = 7 >> a(2,3)=70 %set element to 70 a = 1 2 3 4 5 6 70 8 9 10 11 12 >> a(2,3) %is element in row 2 and column 3 >> a(2,3)=70 % you change its value to the one on the right of the = (assignment sign) >> a(10,10) % does not exist >> a(2,end) % is elt in row 2 and column 4 ; end as column index evaluates to number of columns >> a(end,2) % is elt in row 3 and column 2 ; end as row index evaluates to number of rows >> a(end,end) % is elt in row 3 and column 4 ; end evaluates to 3 and 4 as number of rows, columns % depending on the context >> a(end, end) % end is a variable; if used as row index is number of rows % col index is number of colums VIEWING a MATRIX as an ARRAY **************************** >> a(:) % the colon will be explained later... ans = 1 5 9 2 6 10 3 70 11 4 8 12 % This how a is stored inside the memory of a computer; column major representation % the 8th element can be retrieved by typing a(8) one index inside parentheses. >> a(8) % ARRAY indexing , 8-th element in COLUMN MAJOR order a(2,3) is a(8) i.e. 70 ans = 70 >> a(end) % end as array index evaluates to number of elements i.e. 12 a(12) is 12 ans = 12 ************************************************************************ D. Geometry/shape of a matrix in MATLAB: length and size main diagonal of a matrix , diagonal of a matrix diagonal matrix all-one matrix (ones) all-zero matrix (zeros) upper triangular , lower triangular extracting upper/lower triangular submatrix (triu,tril) identity matrix (eye) random matrix (rand) symmetric matrix prod sum min, max sort help ************************************************************************ In matlab type >> help length and will give information about the command following help (space between help and length) >> length(a) % max{n,m} for a mxn >> size(a) % rows and columns [m n] >> size(a,1) % number of rows m >> size(a,2) % number of columns n >> clear >> a = [1 2 3 4; 5 6 7 8; 9 10 11 12] ; >> length(a) >> size(a) ans = 3 4 % size(a) is a 1x2 row vector/matrix >> size(a,1) >> size(a,2) >> diagonal OF A matrix or more appropriately main diagonal of a matrix: elements whose row index is equal to column index i.e. a(1,1), a(2,2) ... A MATRIX is DIAGONAL if all the elements outside of its main diagonal are 0. (the elements of the main diagonal can be zero or not). diag(a) is MATLAB function to retrieve as a vector the diagonal elements of a >> diag(a) % main diagonal >> diag(a,0) % main diagonal >> diag(a,1) % the 'diagonal' above the main diagonal >> diag(a,-1) % the 'diagonal' below the main diagonal BE CAREFUL >> diag(diag(a))) % creates a diagonal matrix whose diagonal is the diagonal of a !! diagonal matrix can be an arbitrary mxn matrix diagonal of a matrix is a vector i.e. a kx1 matrix If a is mxn then k=min(n,m) ! ones : An all-one matrix of MATLAB ones is ones(1) ones(n) nxn matrix of ones [default: double] ones(m,n) mxn matrix of ones [default: double] ones(m,n,'int8') mxn matrix of ones in int8 data type. Replace last argument appropriately with other numeric data types (single, double, uint8, uint16, uint32, uin64, etc zeros : An all-zero matrix Same syntax as ones MATLAB function eye : Identity matrix , main diagonal is 1 everything else 0. Sometimes written as I I_n I_n,m , I(n) , I(n,m) (Notation: _ is for subscript) Same syntax as ones MATLAB function rand : Random numbers drawn uniformly at random from open interval (0,1) Same syntax as ones MATLAB function Upper triangular matrix : matrix whose elements below diagonal are 0 Lower triangular matrix : matrix whose elements above diagonal are 0 MATLAB supports triu(a) : retrieves the elements at diagonal and above; everything below set to 0 tril(a) : retrieves the elements at diagonal and below; everything above set to 0 A matrix is symmetic if and only if a is equal to transpose(a). sum , prod : a row vector computing the sum/product of the columns of a matrix >> prod(a) % row vector with products of columns >> prod(a') % same for rows >> sum (a) % row vector with products of columns >> sum(a') % same for rows. >> prod(size(a)) % number of elements of matrix a Some examples >> a=[1 2 3 ; 4 5 6 ] a = 1 2 3 4 5 6 % Transpose operators are a.' and transpose(a) a.' >> a.' ans = 1 4 2 5 3 6 >> transpose(a) ans = 1 4 2 5 3 6 >> a' % is the conjugate transpose; however if elements of a are real number it works fine ans = 1 4 2 5 3 6 >> a = a * i % tricky: multiply every element of a with i (i is sqrt of -1) a = 0.0000 + 1.0000i 0.0000 + 2.0000i 0.0000 + 3.0000i 0.0000 + 4.0000i 0.0000 + 5.0000i 0.0000 + 6.0000i >> a' >> a' % beware of the minuses % complex conjugate transpose : a+bi becomes a-bi at the same time rows become columns % and columns rows. ans = 0.0000 - 1.0000i 0.0000 - 4.0000i 0.0000 - 2.0000i 0.0000 - 5.0000i 0.0000 - 3.0000i 0.0000 - 6.0000i >> a.' % everything is ok % transpose : ordinary transpose everything is fine same as transpose(a) ans = 0.0000 + 1.0000i 0.0000 + 4.0000i 0.0000 + 2.0000i 0.0000 + 5.0000i 0.0000 + 3.0000i 0.0000 + 6.0000i >> a = [ 1 2 3 4; 5 6 7 8 ; 9 10 11 12] ans = 1 2 3 4 5 6 7 8 9 10 11 12 >> diag(a) % the diag(a) is a vector of the diagonal elts of a ans = 1 6 11 diag(diag(a)) % but diag(diag(a)) is a square matrix! ans = 1 0 0 0 6 0 0 0 11 diag(a) % diag means main diagonal, i.e. all elts such as row_index == col_index ans = 1 6 11 diag(a,1) % off main diagonal diagonal elements can be accesses as well! ans = 2 7 12 diag(a,2) ans = 3 8 diag(a,-1) ans = 5 10 help diag %MATLAB help !! b = 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 triu(b) ans = 1 2 3 4 5 0 4 6 8 10 0 0 9 12 15 0 0 0 16 20 0 0 0 0 25 tril(b) ans = 1 0 0 0 0 2 4 0 0 0 3 6 9 0 0 4 8 12 16 0 5 10 15 20 25 b= ones(3,4) b = 1 1 1 1 1 1 1 1 1 1 1 1 c= zeros(3,4,'int8'); whos b c Name Size Bytes Class Attributes b 3x4 96 double c 3x4 12 int8 eye(4,5) ans = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 zeros(5,5) ans = 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 rand(5,5) ans = 0.8147 0.0975 0.1576 0.1419 0.6557 0.9058 0.2785 0.9706 0.4218 0.0357 0.1270 0.5469 0.9572 0.9157 0.8491 0.9134 0.9575 0.4854 0.7922 0.9340 0.6324 0.9649 0.8003 0.9595 0.6787 help rand Examples: Example 1: Generate values from the uniform distribution on the interval [a, b]. r = a + (b-a).*rand(100,1); Example 2: Use the RANDI function, instead of rand, to generate integer values from the uniform distribution on the set 1:100. r = randi(100,1,5); Note: floor(4.5) ans = 4 ceil(4.5) ans = 5 round(4.5) ans = 5 % round(x)=floor(x+0.5) ans = 0.8147 0.0975 0.1576 0.1419 0.6557 0.9058 0.2785 0.9706 0.4218 0.0357 0.1270 0.5469 0.9572 0.9157 0.8491 0.9134 0.9575 0.4854 0.7922 0.9340 0.6324 0.9649 0.8003 0.9595 0.6787 round(ans) ans = 1 0 0 0 1 1 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 a=eye(3) a = 1 0 0 0 1 0 0 0 1 a' ans = 1 0 0 0 1 0 0 0 1 a is a a symmetric matrix a' = a ! ************************************************************************ E. MATLAB is a weakly type dynamically allocating language ATTENTION !!! CAUTION !!! ************************************************************************ >> a a = 1 2 3 4 5 6 70 8 9 10 11 12 Tricky >> a(10,10) % does not exist BUT >> a(10,10) = 100 % MATLAB is a WEAKLY TYPED LANGUAGE. THIS WORKS % THIS WOULDNOT HAVE WORKED IN A STRONGLY TYPED LANGUAGE >> a(10,10) = 100 % MATLAB is weakly typed so it would % 1/ Generate a 10x10 matrix and fillit with zeroes : 100operations % 2/ Copy old a into it : 12operations % 3/ set a(10,10) to 100 : 1operations % 4/ delete 3x4 a and rename new one into a % total cost is 113 operations!!! % What if >> a(100000,100000) = 100 % Total cost would be 10,000,000,111 operations (after 10x100 replaced % 3x4 original a !!!! >> % Be careful % EVEN MORE DANGEROUS clear pi ans = 3.1416 pi(4)=10 pi = 0 0 0 10 pi(4,4)=100 pi = 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 100 pi(4,4,4)= 100 % 3-dimensional matrix pi(:,:,1) = 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 100 pi(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 pi(:,:,3) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 pi(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 % And if you want to be more adventurous define a 4 dimensional matrix !! pi(4,4,4,4)=1000 size(pi) ans = 4 4 4 4 MATLAB is an interpreted language: some (more) CAVEATS ******************************************************** Yes, We can run a matlab M-file such as test1.m by calling it (minum its .m suffix >> test1 But for a compiled strongly typed language such as C / Java more is needed 1/ Edit file (eg test1.c) 2/ Compile % gcc test1.c -o test1 3/ Run % ./test1 4/ If an error occurs repeat steps 1/ to 3/ Benefits of an interpreted language: Simplicity, ease of use Benefits of a compiled language: Efficiency for ii = 1:100000 a(ii) = ii*ii; end is a for loop in MATLAB. It tells MATLAB to compute the squares of all integers from 1 to 100000. In addition it stores each partial result into an array/matrix. The second line will be executed 100000. Thus it will be interpreted 100000 times as well. A compiler will only compile this three line program only once, once per line. ************************************************************************ F. MATRIX OPERATIONS and ARRAY OPERATIONS in MATLAB ************************************************************************ ************************************************** - matrix input and matrix view and array view - Dynamic allocation/reallocation and retrieving elements of a matrix - ones zeros eye - Matrix Operator Scalar or Scalar Operator Matrix or Operator(scalar) - Use of sum operator, max, min, sort - Matrix ArrayOperator Matrix - Vector inner product: preparation for matrix multiplication - Matrix multiplication - Martix inversion - Review Inf NaN ***************************************************** a=[1 2 3 4; 5 6 7 8 ; 9 10 11 12 ]; % A 3x4 matrix and its size info whos Name Size Bytes Class Attributes a 3x4 96 double ans 1x1 8 double a = 1 2 3 4 5 6 7 8 9 10 11 12 type >> a(:,:) % matrix view of matrix a >> a(:) : column-major i.e. array view of matrix a as a vector >> a(1:end) : column-major view as a row vector >> a(1:end)' : same as a(:) a= 1 2 3 4 5 6 7 8 9 10 11 12 a(10,10)=123 a = 1 2 3 4 0 0 0 0 0 0 5 6 7 8 0 0 0 0 0 0 9 10 11 12 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 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 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 123 >>a=[1 2 3 4; 5 6 7 8 ; 9 10 11 12 ]; % A 3x4 matrix and its size info ******************************* ******************************* ******************************* % Operation: MATRIX times/plus/minus/etc SCALAR i.e. MATRIX OPERATOR SCALAR % OPERATOR(MATRIX) % a + 10 , a - 10 , a * 10 , a / 10 , but a \ 10 won't work % sin(a), etc ******************************* ******************************* >> a + 10 % MATRIX plus scalar ans = 11 12 13 14 15 16 17 18 19 20 21 22 >> a - 10 % MATRIX minus scalar ans = -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 >> a * 10 % MATRIX times scalar ans = 10 20 30 40 50 60 70 80 90 100 110 120 >> a / 10 % MATRIX / scalar ans = 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 >> a \ 10 % BUT .... Error using \ Matrix dimensions must agree. >> 10-a ans = 9 8 7 6 5 4 3 2 1 0 -1 -2 >> 10*a ans = 10 20 30 40 50 60 70 80 90 100 110 120 >> 10 / a % Out of luck but Error using / Matrix dimensions must agree. >> 10 \ a ans = 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 >> sin(a) ans = 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 -0.5440 -1.0000 -0.5366 >> log(a) ans = 0 0.6931 1.0986 1.3863 1.6094 1.7918 1.9459 2.0794 2.1972 2.3026 2.3979 2.4849 >> exp(a) ans = 1.0e+05 * 0.0000 0.0001 0.0002 0.0005 0.0015 0.0040 0.0110 0.0298 0.0810 0.2203 0.5987 1.6275 ******************************* ******************************* ******************************* % Operation: Matrix relational-operator scalar % a > 5 % a == 5 ******************************* ******************************* a = 1 2 3 4 5 6 7 8 9 10 11 12 >> a > 5 % Result of a>5 is a 3x4 matrix of logical values % 0 : an entry is NOT > 5 (i.e. IS <= 5) ans = % 1 : an antrys IS > 5 0 0 0 0 0 1 1 1 1 1 1 1 >> whos ans Name Size Bytes Class Attributes ans 3x4 12 logical >> sum(sum(ans)) % finds the number of elements greater than 5 ans = 7 >> a == 5 ans = 0 0 0 0 1 0 0 0 0 0 0 0 >> whos a Name Size Bytes Class Attributes a 3x4 96 double >> whos ans Name Size Bytes Class Attributes ans 3x4 12 logical ******************************* ******************************* % min max sort ******************************* ******************************* f= [ 100 50 10 40 70 201 80 ] f = 100 50 10 40 70 201 80 min(f) % min with no values to be retrieved % return the min elements of the vector ans = 10 max(f) % max for max ans = 201 [z1 z2] = min(f) % you can also retrieve the value and the index % in which this value appears !!!!!! z1 = 10 z2 = 3 [z1 z2] = max(f) % you can also retrieve the value and the index % in which this value appears !!!!!! z1 = 201 z2 = 6 >> sort(f) % sort the elements of f , left to right , smallest to largest ans = 10 40 50 70 80 100 201 ******************************* ******************************* % Array operations % c = a + b % c = a - b % c = a .* b % c = a ./ b % c = a .\ b % a,b must have same shape (dimensions) % result is of same shape and % generated by applying operator % corresponding element-wise ******************************* ******************************* a = 1 2 3 4 5 6 7 8 9 10 11 12 b = 1 1 1 1 1 1 1 1 1 1 1 1 a+b % Array operation + arguments are matrices of same geometry ans = 2 3 4 5 6 7 8 9 10 11 12 13 a-b % Array operation - similarly for arguments! ans = 0 1 2 3 4 5 6 7 8 9 10 11 a .* b % Array operation mult is denoted .* not * % Array division/backdivision are ./ and .\ ans = 1 2 3 4 5 6 7 8 9 10 11 12 >> a./b ans = 1 2 3 4 5 6 7 8 9 10 11 12 >> a.\b ans = 1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 >> a.^b % Exponentiation also works! ans = 1 2 3 4 5 6 7 8 9 10 11 12 ******************************* ******************************* % Matrix operations % c = a + b same with array operation % c = a - b same with array operation % c = a * b % if a is m x n % b must be n x p for arbitrary p % and then the result c % would be m x p % [ inner dimensions of a,b must coincide] % [ for * to be defined] % [ c is of share determined by outter dimensions of a,b] % c = a / b % c = a \ b % a,b must be square and so will c IF IT EXISTS! ******************************* ******************************* %%% MATRIX MULTIPLICATION.... PREPARATORY ... x = [ 1 2 3] ; % a row vector y= [ 10 20 30 ]'; % a vector y the transpose of row vector [10 20 30]; x = 1 2 3 y = 10 20 30 x * y % inner/dot/vector product !! % x is 1x3 y is 3x1 result is 1x1 ans = 140 y * x % y is 3x1 x is 1x3 result is 3x3 ans = 10 20 30 20 40 60 30 60 90 % Note that for both operations the result has geometry related to the outer dimensions 1x3 and 3x1 gives 1x1 3x1 and 1x3 give 3x3 but inner dimenstion are the same ..x3 3x.. or ..x1 1x... % This happens in MATRIX MULTIPLICATION IN GENERAL A = B x C (m,n) (m,p) (p,n) ^ ^ ^ ^_____^ ^ | | | | | |_____|_________| | | ________^ The (i,j)-the element of A is A(i,j) = sum from k=1 to p B(i,k) * C(k,j) i takes m values j takes n values for an mxn A k takes p values A(i,j) requires p Mults and p-1 Adds All of A requires pmn Mults and (p-1)mn Adds % One final example for matrix multiplication a is 2x3 and b is 3x2 a= [ 1 2 3 ; 7 8 9] a = 1 2 3 7 8 9 b = [ 4 5; 11 10 ; 1 1 ] b = 4 5 11 10 1 1 a * b ans = 29 28 125 124 % inv(A) is such that A* inv(A) = inv(A) * A = I (for the identity matrix % A must be square. INVERSE of a MATRIX x = [ 1 2 3 ]; y = [ 10 20 30 ]'; x x = 1 2 3 y y = 10 20 30 x * y % x is 1x3 y is 3 x1 result is 1x1 ans = 140 y * x % y is 3x1 x is 1x3 result is 3x3 ans = 10 20 30 20 40 60 30 60 90 inv(y*x) % inv(B) is the inverse of matrix B if and only if inv(B)*B = B*inv(B) = I % Sometimes inverses do not exist ... even if matrix is square >> inv(zeros(4)) Warning: Matrix is singular to working precision. ans = Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf A better example on matrix inversion >> A = [ 5 7 ; 1 1 ] % Solve system of equation Ax = b % For number it would have been x = b/A % but in linear algebra it is x = inv(A) * b A = 5 7 1 1 >> inv(A) ans = -0.5000 3.5000 0.5000 -2.5000 >> A*inv(A) ans = 1.0000 0.0000 0 1.0000 >> inv(A)*A ans = 1.0000 0.0000 0 1.0000 >> b = [ 17 ; 3] %Solve Ax=b ---> x = inv(A) * b b = 17 3 >> x = inv(A) * b x = 2.0000 1.0000 >> A*x ans = 17.0000 3.0000 >> x = A \ b % or x= A \ b x = 2.0000 1.0000 *********************************************************************** >> clear >> a= [ 3 5 8 7 ; 4 2 1 7 ; 8 4 6 7 ; 5 3 1 7] a = 3 5 8 7 4 2 1 7 8 4 6 7 5 3 1 7 >> b= [ 1 0 2 8 ; 5 8 9 2 ; 3 4 0 3; 7 7 7 7 ] b = 1 0 2 8 5 8 9 2 3 4 0 3 7 7 7 7 >> a / b ans = 1.0000 1.0000 -0.0000 -0.4286 0.1412 -0.6706 0.1529 0.9647 -0.6471 -1.1765 -1.1176 2.5546 -0.0471 -0.7765 0.2824 1.1546 >> a \ b ans = 0.2353 0.3235 -0.8824 -0.5294 1.7647 -1.3235 -1.1176 5.5294 -1.2941 -0.5294 -0.6471 -1.5882 0.2605 1.4118 2.2017 -0.7647 >>inv(a) ans = -0.1471 -0.0882 0.2059 0.0294 0.1471 -0.9118 -0.2059 0.9706 0.0588 0.2353 0.1176 -0.4118 0.0336 0.4202 -0.0756 -0.2353 >> inv(b) ans = -0.2941 -0.3529 -0.2353 0.5378 0.1059 0.2471 0.3647 -0.3479 0.0353 0.0824 -0.2118 0.0269 0.1529 0.0235 0.0824 -0.0739 >> a/b ans = 1.0000 1.0000 -0.0000 -0.4286 0.1412 -0.6706 0.1529 0.9647 -0.6471 -1.1765 -1.1176 2.5546 -0.0471 -0.7765 0.2824 1.1546 >> a*inv(b) ans = 1.0000 1.0000 -0.0000 -0.4286 0.1412 -0.6706 0.1529 0.9647 -0.6471 -1.1765 -1.1176 2.5546 -0.0471 -0.7765 0.2824 1.1546 o % What is Inf NaN and inf and nan % inf and nan are variables/ in MATLAB with values Inf and NaN % the value Inf is for infinity (a limit, not a number) % the value NaN is for Not a Number (when you operate with Inf eg Inf-Inf) % You get Inf as a result of 4/0 or -Inf for -4/0 % inf ans = Inf nan ans = NaN 5/ 0 ans = Inf 0 / 0 ans = NaN % NaN can be + or - infinity and thus a NaN can be or might not be equal to another Nan % Thus NaN == NaN ans = 0 % isnan and isinf are two MATLAB functions that test for Inf or NaN an expression. isnan(0/0) ans = 1 isinf(5/0) ans = 1 ************************************************************************ G. COLON OPERATOR : ************************************************************************ ************************************************** % Colon operator a:b:c % Colon operator a:c is a:1:c % Colon operator a:b:c for positive b is a a+b .... a+ib as long as a+ib <= c % Colon operator a:b:c for negative b is a a+b .... a+ib as long as a+ib >= c % % Note that 1:1:0 is empty matrix 1x0 % 1:-1:0 is [1 0] % 1:10 and [ 1:10] give the same answer >> 1 : 10 ans = 1 2 3 4 5 6 7 8 9 10 >>1 : 1 : 10 ans = 1 2 3 4 5 6 7 8 9 10 >>0:2:10 % even numbers between 0 and 10 (inclusive of endpoints) ans = 0 2 4 6 8 10 >> 2:2:10 ans = 2 4 6 8 10 % odd numbers between 1 and 10 >>1:2:10 ans = 1 3 5 7 9 >>1:-1:0 ans = 1 0 >>1 : 1 : 0 ans = Empty matrix: 1-by-0 % Function linspace vs colon operator % colon operator generates numbers in an interval when STEP is known % linspace generates number in an interval when number of points is KNOWN! % linspace(a,b,n) generates n equi-spaced points between a and b ; step is computed (a-b)/(n-1) linspace(0,1,10) ans = Columns 1 through 7 0 0.1111 0.2222 0.3333 0.4444 0.5556 0.6667 Columns 8 through 10 0.7778 0.8889 1.0000 % Colon operators, vector filling, and plot function x= 0:0.2:5; y= sin(x); plot(x,y); plot(y,x) *********************** OTHER USES OF COLON OPERATOR: Submatrices *********************** clc a=zeros(5,5);b=1:25; clc a(:)=b(:); a a = 1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25