clear % variables -- no definition just use! a = 3; b = 4; % MATLAB is case sensitive A = a + b; % addition B = a - b; % subtraction C = a * b; % multiplication D = a / b; % division E = a ^ b; % power % whos -- show the variables in the workspace % omitting the semi column ; shows the value of the expression/variable clear; % vectors: matrices with either number of rows or columns is 1 % creating vectors x = [2 8 9 5] % row vector y = ones(5,1) % column vector z = zeros(1,4) % row vector t = [4;6;9;1;0] % column vector k = 5:2:10 % start with 5, increment by 2 until 10 m = 3:12; % start with 3, use default increment (1) until 12 % create 20 linearly spaced points between 5 and 15 lin = linspace(5,15,20); % create 15 logarithmically spaced points between 10^2 and 10^4 log = logspace(2,4,15); % show sizes size(k) [numrows, numcols] = size(m) %accessing vector elements -- indices start with 1 x(3) y(4) t(2:4) y(1:2:5) %x(6) % out of bounds --- error %x(5) % out of bounds --- error x(6) = 8; % not an error, but slows down the process. Better to use preallocated sizes x disp(x) % display % assignment operator l = k %row vector % transpose operator l = k' %column vector clear all % clear all memory % matrix % creating matrix A = [1 2; 3 4]; B = [1,3,4; 5,8,9]; D = zeros(3,5); % D is a matrix with 3 rows, 5 columns, cells initialized to 0 E = ones(2,7); C = [ 1 4 5 3 6 8 9 7 9 11 2 5]; C %access element a = C(2,4); %re-assign value C(2,4) = -13; a C size(C) %C(3,6) -- out of bounds % assign C(3, 6) = 22; disp(C) % + - * / \ ^ ' % .* ./ .\ .^ .' x= [1,2,3]; y=[4,5,6]; x+i*y (x+i*y)' (x+i*y).' % matrix concatenation Xr = [x y]; Xc = [x; y]; % strings first = 'John'; last = 'Coltrane'; name = [first,' ',last] save wrkspc clear load wrkspc whos save somevars Xr Xc name clear load somevars whos