%% basic arithmetic 1+1 1+1; % semicolon supresses output 1/2 2^4 %% variables and vectors/matrices a = 1 b = pi a+b A = [1 2; 3 4; 5 6] A' B = [3 4; 5 6; 7 8]; A + B A - B A * B % (invalid) A * B' A .* B A .^ 2 A ^ 2 % (invalid) (A'*A)^2 x = [1 2 3 4] x = [1; 2; 3; 4] x = [1 2 3 4]' x = 1:4 %% special matrices A = ones(2,3) A = zeros(2,3) A = rand(2,3) A = randn(2,3) A = eye(4) A = diag(x) x = diag(eye(4)) A = ones(2,3) size(A) size(A,1) whos clear A %% indexing elements x = randn(5,1) x(3) x(3:4) A = rand(2,3) A(1,:) A(1,2:3) A(1,2:end) A(1,:) = [1 2 3] %% complex numbers A = [1+2*i 3+4*i; 5+6*i 6+7*i] A = [1+2*j 3+4*j; 5+6*j 6+7*j] A' A.' %% some built-in functions exp(2) log(1) sin(pi) abs(-2) A = randn(2,3) exp(A) expm(A'*A) % don't worry about what this is doing disp('a string'); disp(['Size of A: ' num2str(size(A,1)) 'x' num2str(size(A,2))]); help exp format long A format short A %% plotting x = 0:0.1:2*pi; plot(x, sin(x)); plot(x, sin(x), 'g--x'); plot3(x,sin(x),cos(x)); y = 0.0:0.1:2*pi; [X,Y] = meshgrid(x,y); Z = cos(X) + sin(Y); surf(X,Y,Z) %% program control x = zeros(10,1); for i=1:size(x,1) x(i) = i; end if (size(x,1) > 50) disp('x is big') else disp('x is small') end if (size(x,1) > 500) disp('x is big') elseif (size(x,1) > 50) disp('x is medium') else disp('x is small') end while (size(x,1) < 50) x(end+1) = size(x,1)+1; end %% functions function y = square_and_double(x) y = 2*(x.^2) square_and_double(4) A = randn(5,4) square_and_double(4)