Important/useful Matlab commands/functions
    (do examples on Matlab!)

  • arrays
    x = zeros( 1 , N );   x = ones( 1, N );
    x = rand( 1 , N );   x = randi( [5, 45], 1, N );

      s = sort(x);

    Sum = sum( x );   [Min, iMin] = min( x );   [Max, iMax] = max( x );
    write Matlab function 'myMin(x)' using loop,if, to do what 'min(x)' does

  • But on a matrix A, these act column-wise, e.g.
    Sum = sum( A ) = array of column sums = [ sum(A(:,1))   sum(A(:,2))   ... sum(A(:,N)) ]
      To find sum of all entries of a matrix A, can use: Sum = sum( A(:) );
      For an M×N matrix A, A(:) = columns of A stacked in one column ( MN×1 column array )

  • Grid points
    x = linspace( a , b, M+1 ) for M subintervals, each of width dx=(b-a)/M
    x = a : dx : b ;

  • I/O
    a = input( 'enter a: ' )   for numerical input
    word = input( 'enter word: ', 's' )   for string input
    fprintf(' ... formating ... \n', vars )   prints on ONE line, \n for new line

  • plotting :   ( note: x must be sorted! )
    plot( x , y , '-r' )   title('Title')   xlabel('x')   ylabel('y')

  • function m-file in a file FCN.m :
    function [ y ] = FCN( x )
        . . . ;
        y = ... ;
        . . . ;
      end %function