% This file is a tutorial "script" % it is a "script" as opposed to a "function" % .. typing its name (without the .m) at the matlab prompt % will just execute all the lines of this script in order. % you can try typing all these to see what happens % tip: not ending a line in a semicolon displays the result % clear all variables in matlab memory clear all; a = 3; % save a variable b = 3; % and another whos % see which variables are there clear a; % clear just a from memory whos % see which variables are there (only b) clear all; % clear everything again % basic arithmetic 3 * 3 % can multiply 3 ^ 3 % exponentiate mod(5,3) % compute 5 mod 3 0 < 1 % compare numbers 0 < inf % compare to infinity 0 < -inf % or negative infinity nan < inf % nan will not satisfy any inequality % matrices % manual entry M = 2; % "1x1" matrix N = [ 2 ; 2 ]; % 2x1 matrix -- semicolon = new row O = [ 1 2 ; 3 4 ]; % spaces are new columns P = [ 1 2 % can also use a newline to create new row 3 4 ]; % usual initialization functions % NOTE: If you have a hard time remembering what a 3x4 matrix % is, (whether 3 columns or 3 rows), remember "Roman Catholic": % Rows, then Columns! a = ones(3); % 3x3 matrix of all ones b = zeros(3,4); % 3x4 matrix of all zeros c = ones(2,2,2); % 2x2x2 matrix of all ones d = eye(4); % 4x4 identity matrix e = rand(2); % 2x2 random uniform matrix f = randn(2); % 2x2 random zero-mean unit-std gaussian matrix % iterator examples g = [ 1 : 5 ]; % two operands h = [ 10 : -2.5 : 0 ]; % three operands % linspace i = linspace(0,10,9); % 9 values evenly spaced in [0,10] % repmat j = repmat( ones(2) , [ 1 2 ] ); % 2 x 4 matrix k = repmat( ones(2) , [ 2 1 ] ); % 4 x 2 matrix l = repmat( ones(2) , [ 2 2 ] ); % 4 x 4 matrix % matrix concatentation examples m = zeros(4,4); % 4 x 4 all zeros m(:) = 1 : 16; % assign the values 1:16 to elements of m m(eye(4)==1)=0; % set diagonal elements to 0 n = -m'; % n is negative transpose (') of m mcatn = [ m n ]; maboven = [ m ; n ]; ocatz = [ ones(3,3) ; zeros(3,3) ]; % structures % just declare a "structure" named "o" and give it some "fields" o.name = 'alice'; % introduced a string o.name(1) % the character 'a' o.height = 70; % cell arrays, use curly brace instead of parentheses to index p{1} = rand(2,2); % each element of the cell array can be anything at all p{2} = o; p{3} = -1; save aandm.mat a m % save variables a and m to disk amstruct = load('aandm.mat'); % load a and m into struct named amstruct % note, if we had just used: % load('aandm.mat') % the variables 'a' and 'm' would have appeared in our matlab environment % without any container struct (e.g. 'amstruct' above) % basic matrix arithmetic m % no semicolon, so this variable is displayed n % and this npm = n + m; % you can add matrices ndivm = n / m; % or divide ninvm = n * inv(m); % which is same as multiplying by the inverse npwdivm = n ./ m; % divide matrices, element by element npwmulm = n .* m; % pointwise multiply nm = n * m; % full matrix multiplication