MATLAB BASICS In Matlab, a variable may be either a scalar (a regular single number), a vector (row or column of numbers) or a matrix (square or rectangular array of numbers) % Define some row vectors: >> a=[1 1 1 1 1]; >> b=[2 2 2 2 2]; >> c=[3 3 3 3 3]; % The "size" function lists the number of rows first, then the number of columns >> size(a) ans = 1 5 % A single quote flips rows and columns of a vector or matrix (called the "transpose") >> a' ans = 1 1 1 1 1 >> size(a') ans = 5 1 % Ways of combining 2 vectors into one vector or matrix >> [a b] ans = 1 1 1 1 1 2 2 2 2 2 >> [a;b] ans = 1 1 1 1 1 2 2 2 2 2 >> size([a;b]) ans = 2 5 >> [a,b] ans = 1 1 1 1 1 2 2 2 2 2 >> [a' b'] This results in a matrix" ans = 1 2 1 2 1 2 1 2 1 2 >> [a',b'] ans = 1 2 1 2 1 2 1 2 1 2 >> [a';b'] ans = 1 1 1 1 1 2 2 2 2 2 >> [a' b] ??? Error using ==> horzcat CAT arguments dimensions are not consistent. % Multiplying and dividing two vectors >> a/b ans = 0.5 % Inserting a period before performs a point-by-point operation >> a./b ans = 0.5 0.5 0.5 0.5 0.5 >> a\b ans = 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 >> a*b ??? Error using ==> mtimes Inner matrix dimensions must agree. >> a.*b ans = 2 2 2 2 2 >> a'*b ans = 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 >> a*b' ans = 10 >> b.^2 ans = 4 4 4 4 4 >> b^2 ??? Error using ==> mpower Matrix must be square. ------------------------------------------------------------------------------------------- The ORDER OF OPERATIONS is somtimes significant, For example a./b.*c is not the same as a./(b.*c). >> a./b.*c ans = 1.5000 1.5000 1.5000 1.5000 1.5000 >> a./(b.*c) ans = 0.1667 0.1667 0.1667 0.1667 0.1667 It works the same with scalar and arrays variables. When in doubt, use parentheses to force the order that you want. ------------------------------------------------------------------------------------------ LOGICAL OPERATORS such as "greater than" (>), "less than" (<), and "equal to" (==) also works with vectors and matrices, returning a vector or matrix of the same size with zeros for the element that don't match that conditions and ones for those that do. >> M=[1 2 3 4 5;2 3 4 5 6;3 4 5 6 7;4 5 6 7 8;5 6 7 8 9] M = 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 >> M>5 ans = 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 1 1 1 0 1 1 1 1 >> M<6 ans = 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 >> M==5 ans = 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 As a practical example of the latter, consider a peak table P, in which the third column is the peak height. P = 1.0000 0.6700 30.6182 24.9063 0.6589 0.4311 2.0000 0.7031 22.8572 14.9694 0.5187 0.2412 3.0000 0.7417 22.0156 17.6447 0.5182 0.3405 4.0000 0.8868 43.0598 32.0137 1.1361 0.6465 5.0000 0.9259 44.9324 23.3539 1.2205 0.4159 6.0000 0.9593 46.4616 26.9186 1.3063 0.5177 7.0000 1.0169 26.1579 18.9237 0.7986 0.4305 8.0000 1.0660 30.1137 24.5057 0.9178 0.6000 These statements use the downloadable "val2ind" function to return that portion of P that have peaks higher that 40. >> j=P(:,3)>40;k=val2ind(j,1);P(k,:) ans = 4.0000 0.8868 43.0598 32.0137 1.1361 0.6465 5.0000 0.9259 44.9324 23.3539 1.2205 0.4159 6.0000 0.9593 46.4616 26.9186 1.3063 0.5177