% Simple matlab operations % Print a matlab built-in variable pi ans = 3.1416 path MATLABPATH C:\Matlab7\sptr_toolkit C:\MATLAB7\sptr_Toolkit\bayes C:\MATLAB7\sptr_Toolkit\data C:\MATLAB7\sptr_Toolkit\data\anderson_task . . . % Adding your own directory to the load path P=path P = path (P,'J:/www'); % Creating some variables - scalar, string, vector, matrix x = 5 x = 5 name = 'Alley Oop' name = Alley Oop % A row vector xr=[4 1 -3 6 4 1] xr = 4 1 -3 6 4 1 % A column vector using a semi-colon to stop annoying print xc=[-2;3;1;1;0;5]; % Now print it anyway xc xc = -2 3 1 1 0 5 m=[1 2 3;4 5 6;7 8 9]; m m = 1 2 3 4 5 6 7 8 9 % Perform some operations on the variables. x+4 ans = 9 xr/2 ans = 2.0000 0.5000 -1.5000 3.0000 2.0000 0.5000 % Transpose the vector xr xr = 4 1 -3 6 4 1 xr' ans = 4 1 -3 6 4 1 % Operations on vectors sum (xr) ans = 13 prod (xr) ans = -288 sort (xc) ans = -2 0 1 1 3 5 % Append some elements xr=[xr 0 4 2 5] xr = 4 1 -3 6 4 1 0 4 2 5 % Find the non-zero elements find (xr) ans = 1 2 3 4 5 6 8 9 10 xr (find (xr)) ans = 4 1 -3 6 4 1 4 2 5 % Indexing operations xr (3) ans = -3 xc xc = -2 3 1 1 0 5 xc (3:5) ans = 1 1 0 % Try the power function and then try it element by element. xc^2 ??? Error using ==> mpower Matrix must be square. xc.^2 ans = 4 9 1 1 0 25 % Logical operations xc < 3 ans = 1 0 1 1 1 0 % Matrix operations m m = 1 2 3 4 5 6 7 8 9 m + 1 ans = 2 3 4 5 6 7 8 9 10 m .* 2 ans = 2 4 6 8 10 12 14 16 18 m*2 ans = 2 4 6 8 10 12 14 16 18 m' ans = 1 4 7 2 5 8 3 6 9 % Create another matrix using the colon notation m2=[3:3:9;1:2:5]; m2 m2 = 3 6 9 1 3 5 m + m2 ??? Error using ==> plus Matrix dimensions must agree. m*m2 ??? Error using ==> mtimes Inner matrix dimensions must agree. m2*m ans = 90 108 126 48 57 66 m2=[m2;4 4 6]; m .* m2 ans = 3 12 27 4 15 30 28 32 54 m3 = rand (4,4) m3 = 0.4660 0.2026 0.6813 0.7095 0.4186 0.6721 0.3795 0.4289 0.8462 0.8381 0.8318 0.3046 0.5252 0.0196 0.5028 0.1897 reshape (m3, 2, 8) ans = Columns 1 through 6 0.4660 0.8462 0.2026 0.8381 0.6813 0.8318 0.4186 0.5252 0.6721 0.0196 0.3795 0.5028 Columns 7 through 8 0.7095 0.3046 0.4289 0.1897 max (m) ans = 7 8 9 max (max (m)) ans = 9 prod (m) ans = 28 80 162 rem (m,m2) ans = 1 2 3 0 2 1 3 0 3 diag (m2) ans = 3 3 6 inv (m) Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.541976e-018. ans = 1.0e+016 * -0.4504 0.9007 -0.4504 0.9007 -1.8014 0.9007 -0.4504 0.9007 -0.4504 inv (m2) ans = -0.3333 0 0.5000 2.3333 -3.0000 -1.0000 -1.3333 2.0000 0.5000 det (m2) ans = 6 eig (m2) ans = 13.3110 -0.6555 + 0.1452i -0.6555 - 0.1452i [v,d] = eig (m2) v = -0.7235 -0.2429 + 0.0874i -0.2429 - 0.0874i -0.3568 -0.7555 -0.7555 -0.5910 0.6009 - 0.0394i 0.6009 + 0.0394i d = 13.3110 0 0 0 -0.6555 + 0.1452i 0 0 0 -0.6555 - 0.1452i size (m) ans = 3 3 eye (3,3) ans = 1 0 0 0 1 0 0 0 1 eye (2,4) ans = 1 0 0 0 0 1 0 0 ones (2,2) ans = 1 1 1 1 zeros (2,4) ans = 0 0 0 0 0 0 0 0 rand (4,3) ans = 0.9501 0.8913 0.8214 0.2311 0.7621 0.4447 0.6068 0.4565 0.6154 0.4860 0.0185 0.7919 m (2,2) ans = 5 m(2) ans = 4 m(2,:) ans = 4 5 6 m (1:2, 2:3) ans = 2 3 5 6 m2 = 3 6 9 1 3 5 4 4 6 reshape (sort (reshape (m2,1,9)), 3, 3) ans = 1 4 6 3 4 6 3 5 9