====== Diary of review session ====== x = 1:3 % 1 to 3 by increments of 1 x = 1 2 3 x = 1:3:10 % 1 to 10 by increments of 3 x = 1 4 7 10 x = [ 3 8 12 14 ] % specify elements of a vector manually x = 3 8 12 14 x = [ 3 8 12 14 ]' % ' means transpose: turns a row vector into a col vector x = 3 8 12 14 x = [ 3 ; 8 ; 12 ; 14 ] % semicolons inside [] delimit rows, produces col vector x = 3 8 12 14 A = [ 4 2 1 ; 3 4 5; 7 2 9] % semicolons delimit rows, produces matrix A = 4 2 1 3 4 5 7 2 9 A = [ 4 2 1 ; 3 4 5; 7 2 9]' % ' means transpose, interchange A(i,j) and A(j,i) A = 4 3 7 2 4 2 1 5 9 3 + 4*i % this is how you enter a complex number ans = 3.0000 + 4.0000i i % i is the unit imaginary number ans = 0 + 1.0000i i^2 % i == sqrt(-1), i^2 == -1 ans = -1 z = 3 + 4*i % assign complex number to z z = 3.0000 + 4.0000i A = [ 4 2 1 ; 3 4 5; 7 2 9+2*i] % a matrix with a complex element A = 4.0000 2.0000 1.0000 3.0000 4.0000 5.0000 7.0000 2.0000 9.0000 + 2.0000i 1.3e-03 % compact scientific notation, means 1.3 times 10^(-3) ans = 0.0013 4e14 % means 4 x 10^14 ans = 4.0000e+14 4*10^(+14) % same thing ans = 4.0000e+14 % A floating point number has sixteen digits of accuracy. % That means sixteen digits in the MANTISSA (here 1.4) 1.4e75 ans = 1.4000e+75 % The EXPONENT can go up to +308 or -324 1e308 % this is ok ans = 1.0000e+308 1e309 % this is not ok ans = Inf 1/0 % infinity, infinity, how can I reach infinity? ans = Inf -1/0 % another way, downwards ans = -Inf Inf-Inf % this is undefined, so the answer is Not a Number ans = NaN 0/0 % this is undefined, so the answer is Not a Number ans = NaN tan(pi/2) % might guess Inf, but Matlab's pi is rounded to sixteen digits ans = 1.6331e+16 format long pi ans = 3.141592653589793 4 + 7^4 + 0/0*(4+3) % a single NaN in an expression with make the result NaN ans = NaN % elementwise calculations x = [2 1 3]; x = 2 1 3 y = [4 6 9]; y = 4 6 9 x + y % + on vectors means elementwise addtion ans = 6 7 12 x - y % - on vectors means elementwise subtraction ans = -2 -5 -6 % but * on vectors is the innerproduct! x = [2 1 3] x = 2 1 3 y = [4 6 9]' y = 4 6 9 x*y % innerproduct for 3-vectors is x(1)*y(1) + x(2)*y(2) + x(3)*y(3) ans = 41 2*4 + 1*6 + 3*9 ans = 41 % to get component wise multiplication of vectors, use .* sytnax x = [2 1 3] x = 2 1 3 y = [4 6 9] y = 4 6 9 x.*y % componentwise multiplication: ans(i) = x(i)*y(i) ans = 8 6 27 x = [2 1 3]; x.^2 % componentwise square ans = 4 1 9 x./y % componentwise division ans = 0.500000000000000 0.166666666666667 0.333333333333333 0 == 0 && 1 == 1 % true AND true => true ans = 1 0 == 0 && 1 == 0 % true AND false => false ans = 0 0 == 0 || 1 == 0 % true OR false => true zeros(4,5) % makes a 4 x 5 matrix of zeros ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ones(4,5) % makes a 4 x 5 matrix of ones ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 eye(5) % makes a 5 x 5 identity matrix, I ans = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 % rand(4,5) makes a 4 x 5 matrix of random numbers % uniformly distributed btwn 0 and 1 rand(4,5) ans = 0.6557 0.6787 0.6555 0.2769 0.6948 0.0357 0.7577 0.1712 0.0462 0.3171 0.8491 0.7431 0.7060 0.0971 0.9502 0.9340 0.3922 0.0318 0.8235 0.0344 randi(10, 3,2) % makes a 3 x 2 matrix of random integers btwn 1 and 10 ans = 5 8 7 3 8 7 % makes a 3 x 2 matrix of random numbers with a Guassian or normal distrib. randn(3,2) ans = 0.6007 -0.0068 -1.2141 1.5326 -1.1135 -0.7697 x = randn(1000,1); % make a vector of 1000 random numbers in Gaussian dist hist(x) % plot a histogram of x xlabel('value of x') ylabel('number of occurences') title('Histogram of Gaussian distribution') x = rand(1000,1); % make a vector of 1000 random numbers in uniform distrib figure(2) ; % open a new figure window hist(x) title('Histogram of uniform distribution') ylabel('number of occurences') xlabel('valiue of x') % size function A = rand(4,5) A = 0.8844 0.6198 0.1962 0.7985 0.7022 0.4390 0.2606 0.3039 0.9875 0.3755 0.7817 0.4457 0.4833 0.1590 0.9737 0.1485 0.8440 0.3378 0.2369 0.9723 size(A) ans = 4 5 [M N] = size(A) M = 4 N = 5 size(A,1) % return the number of rows in A ans = 4 size(A,2) % return the number of cols in A ans = 5 x = input('please type in a number: ') please type in a number: 34 x = 34 disp('hello, world!') % simple print of input hello, world! % fprintf is for more complicated printing x = 12; y = 3; % print the string argument, substituting the values of x,y in place of %d's % %d marker means decimal variable fprintf('x == %d, y == %d\n', x, y); x == 12, y == 3 x = 'z' x = z % %c marker means character variable fprintf('x == %c\n', x); x == z