Some MATLAB Commands Timing: Tic Start timer Toc Report time in seconds since "toc" command. Functions: Powers: x^y; returns the Y-th power of X Floor: i = floor ( x ): round down to an integer. Sign: s = sign ( x ); +1 for positive, -1 for negative. What happens at zero? Remainder: r = mod ( x, y ); returns the remainder after X is divided by Y. Allocate space: v = zeros ( m, 1 ); creates a column vector of length M w = zeros ( 1, n ); creates a row vector of length N A = zeros ( m, n ); creates an array of zeros of size MxN Set values: v = linspace ( first, last, howmany ); create row vector, evenly spaced values. v = [ 1; 2; 3 ]; creates a column vector. w = [ 1, 2, 3 ]; creates a row vector; A = [ 11, 12, 13; 21, 22, 23 ]; creates a 2x3 matrix. Set random values between 0 and 1: x = rand ( ); a single random number; v = rand ( m, 1 ); random column vector; w = rand ( 1, n ); randow row vector; A = rand ( m, n ); random matrix; B = rand ( m ); actually the same as rand ( m, m ), returns a MATRIX. Append a value: v = [ v, 1 ]; appends "1" to a row vector w = [ w; 1 ]; appends "1" to a column vector; This can also be done with matrices, but be careful! Vector/array functions: Colon notation: 1:n; means [ 1, 2, 3, ..., n ] 1:2:10; means [ 1, 3, 5, ..., 9] 10:-3:0 means [10, 7, 4, 1] v(2:4) means [ v(2), v(3), v(4) ] A(2,:) means row 2 of matrix A A(:,4) means column 4 of matrix A. Sum: s = sum ( v ); returns the sum of the entries in a vector; s = sum ( A ); returns a vector, the sum of each column of A. s = sum ( sum ( A ) ); returns a number, the sum of all entries of A. Minimum/Maximum (work similarly to Sum) v_min = min ( v ) a_min = min ( A ); (column minimums) a_min = min ( min ( A ) ); (matrix minimum) Any: any ( v == 0 ) returns "TRUE" (actually, 1) if any entry of V is less than 1. All: all ( 0 <= w ) returns TRUE if all entries of W are greater than 1. "AND" (&) and "OR" (|) and "NOT" (~) if ( x < 0.0 & y < 0.0 ) ... if ( x == 0.0 | y == x ) ... if ( x ~= 0.0 ) ... FIND: index all entries for which condition is true i = find ( v < 0.0 ) Transpose: MxN matrix becomes NxM matrix. A = A' Length: Number of entries in vector or matrix l = length ( v ); Size: Returns number of rows and columns in vector or matrix [ m, n ] = size ( A ); Vector formulas: Multiplication: norm = sqrt ( v' * v ); if v is a column vector norm = sqrt ( w * w' ); if w is a row vector; v2 = A * v1; if A is MxN and V1 is Nx1 (column vector) w2 = A * w1'; if A is MxN and W1 is 1xN (row vector) Dotted operators: *, /, ^ x = v1' * v2; computes a scalar; v3 = v1 .* v2; multiplies matching pairs of entries. v3 = v1 ./ v2; divides matching pairs of values. v3 = v1 .^ 2; squares each entry of v1. C = A * B; uses matrix multiplication C = A .* B; multiplies matching pairs of entries C = A^2; is the same as C = A * A, matrix multiplication. C = A.^2; squares each entry of A. Solving linear systems: b = A \ x; solves A*x=b for the vector x. Loops: WHILE: do the loop again, if condition is true while ( x < 0.0 ) x = x + 1; end IF / ELSEIF / ELSE ("elseif" has to be one word, not two!) if ( x < 0.0 ) sign = -1.0; elseif ( x > 0.0 ) sign = +1.0; else sign = 0.0; end Printing: To print a variable, you can just name it, with no semicolon at the end: x You can use the "disp" command (which omits the variable name) disp x You can use the "fprintf(1,'STRING',LIST)" command: fprintf ( 1, '%g\n', x ); one real number fprintf ( 1, ' n = %d\n', n ); one integer, with label. fprintf ( 1, ' x(%d) = %g\n', i, x(i) ); an integer and a real. Plotting: plot ( x, y ); plot ( x, y, 'r-' ); plot ( x, y, 'r-', 'Linewidth', 3 ); plot ( x, y, 'b*' ) plot ( x, y, 'b*', 'Markersize', 25 ); grid on title ( 'This is my title' ) clf; (clear the current screen, get rid of previous picture) hold on (I want to use several plot commands, but make a single plot) hold off (I am done with the multiple plots on one image) figure ( ); put this plot in a new figure box. figure ( 3 ): put this plot in figure box 3. Read data from a file: (each row of the file must have the same number of values) xy = load ( 'filename' )