>> % We can capture all our activities in a file with 'diary filename'... >> diary matlab_tutorial.txt >> % GETTING STARTED >> % The last reported answer is given by the name 'ans'... >> 5*5 ans = 25 >> ans ans = 25 >> % All variables hold arrays of something (even a scalar which is 1x1). >> % Specify an array with brackets [], use commas to separate elements and semi-colons to separate rows... >> A=[1,2,3,4; 4,2,1,7; 9,8,9,3; 5,2,8,6] A = 1 2 3 4 4 2 1 7 9 8 9 3 5 2 8 6 >> % Suppress output with a semi-colon at the end of the line... >> B=[1,4,9,5; 2,2,8,2; 3,1,9,8; 4,7,3,6]; >> % The : operator generates a vector with a range of values (start:step:stop)... >> x=1:0.3:2 x = 1.0000 1.3000 1.6000 1.9000 >> % Use symbols i or j to denote the imaginary number... >> 5+7i ans = 5.0000 + 7.0000i >> C=[1+2i,2+3i,3+4i,4+i; 4+2j,2+j,1+7j,7+4j; 9+8i,8+9i,9+3i,3+9i; i 1 j 2] C = 1.0000 + 2.0000i 2.0000 + 3.0000i 3.0000 + 4.0000i 4.0000 + 1.0000i 4.0000 + 2.0000i 2.0000 + 1.0000i 1.0000 + 7.0000i 7.0000 + 4.0000i 9.0000 + 8.0000i 8.0000 + 9.0000i 9.0000 + 3.0000i 3.0000 + 9.0000i 0 + 1.0000i 1.0000 0 + 1.0000i 2.0000 >> % Constants already defined: pi, eps, Inf, NaN >> % Look at variables with 'whos'... >> whos Name Size Bytes Class A 4x4 128 double array B 4x4 128 double array C 4x4 256 double array (complex) ans 1x1 16 double array (complex) x 1x4 32 double array Grand total is 53 elements using 560 bytes >> % Variables can be saved for later use with 'save'; loaded with 'load'; and cleared with 'clear'... >> save myfile.mat x >> clear x >> whos Name Size Bytes Class A 4x4 128 double array B 4x4 128 double array C 4x4 256 double array (complex) ans 1x1 16 double array (complex) Grand total is 49 elements using 528 bytes >> load myfile >> whos Name Size Bytes Class A 4x4 128 double array B 4x4 128 double array C 4x4 256 double array (complex) ans 1x1 16 double array (complex) x 1x4 32 double array Grand total is 53 elements using 560 bytes >> % Matlab is case-sensitive, all functions are lower case. >> % Continue long lines by typing '...' before returning. >> % BASIC OPERATORS >> % Arrays are subscripted with () and indexed from 1... >> A(1,3) ans = 3 >> A(1,:) ans = 1 2 3 4 >> % Arrays can be concatenated by []... >> [A, B] ans = 1 2 3 4 1 4 9 5 4 2 1 7 2 2 8 2 9 8 9 3 3 1 9 8 5 2 8 6 4 7 3 6 >> [A; B] ans = 1 2 3 4 4 2 1 7 9 8 9 3 5 2 8 6 1 4 9 5 2 2 8 2 3 1 9 8 4 7 3 6 >> % Matrix addition and subtraction... >> A+B ans = 2 6 12 9 6 4 9 9 12 9 18 11 9 9 11 12 >> A-B ans = 0 -2 -6 -1 2 0 -7 5 6 7 0 -5 1 -5 5 0 >> % Scalar operations apply to each element individually: 5+A, 5-A, A-5, 5*A, A/5... >> 5+A ans = 6 7 8 9 9 7 6 12 14 13 14 8 10 7 13 11 >> A/5 ans = 0.2000 0.4000 0.6000 0.8000 0.8000 0.4000 0.2000 1.4000 1.8000 1.6000 1.8000 0.6000 1.0000 0.4000 1.6000 1.2000 >> % With matrix multiplication and division, * and / operate on whole matricies while .* and ./ go element-by-element... >> A/B ans = 0.3519 -0.3677 0.2573 0.1529 -0.7233 -0.3374 0.9126 0.6650 -1.8981 3.0073 -0.2427 1.4029 -0.8981 1.0073 0.7573 0.4029 >> A./B ans = 1.0000 0.5000 0.3333 0.8000 2.0000 1.0000 0.1250 3.5000 3.0000 8.0000 1.0000 0.3750 1.2500 0.2857 2.6667 1.0000 >> % If you want to see more detail, use 'format long'. To hide detail, use 'format short'... >> format long >> A./B ans = 1.00000000000000 0.50000000000000 0.33333333333333 0.80000000000000 2.00000000000000 1.00000000000000 0.12500000000000 3.50000000000000 3.00000000000000 8.00000000000000 1.00000000000000 0.37500000000000 1.25000000000000 0.28571428571429 2.66666666666667 1.00000000000000 >> format short >> % Transpose operators: ' is conjugate-transpose and .' is transpose... >> C' ans = 1.0000 - 2.0000i 4.0000 - 2.0000i 9.0000 - 8.0000i 0 - 1.0000i 2.0000 - 3.0000i 2.0000 - 1.0000i 8.0000 - 9.0000i 1.0000 3.0000 - 4.0000i 1.0000 - 7.0000i 9.0000 - 3.0000i 0 - 1.0000i 4.0000 - 1.0000i 7.0000 - 4.0000i 3.0000 - 9.0000i 2.0000 >> C.' ans = 1.0000 + 2.0000i 4.0000 + 2.0000i 9.0000 + 8.0000i 0 + 1.0000i 2.0000 + 3.0000i 2.0000 + 1.0000i 8.0000 + 9.0000i 1.0000 3.0000 + 4.0000i 1.0000 + 7.0000i 9.0000 + 3.0000i 0 + 1.0000i 4.0000 + 1.0000i 7.0000 + 4.0000i 3.0000 + 9.0000i 2.0000 >> % Matrix powers: ^ raise a matrix to a power; .^ raise each element to a power... >> A^2 ans = 56 38 64 51 56 34 79 75 137 112 140 137 115 90 137 94 >> A.^2 ans = 1 4 9 16 16 4 1 49 81 64 81 9 25 4 64 36 >> % Type 'help ops' for more help on operators. >> % BASIC FUNCTIONS >> % Matrix creation functions include: 'ones', 'zeros', 'rand', and 'randn'... >> ones(3,5) ans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >> rand(3,5) ans = 0.9501 0.4860 0.4565 0.4447 0.9218 0.2311 0.8913 0.0185 0.6154 0.7382 0.6068 0.7621 0.8214 0.7919 0.1763 >> % Function 'sum' operates along rows unless you specify otherwise... >> sum(A) ans = 19 14 21 20 >> sum(A,2) ans = 10 14 29 21 >> % Use the : operator to select a sub-matrix... >> sum(A(1:3,2:4)) ans = 12 13 14 >> % Function 'prod' and 'mean' work similarly... >> prod(A) ans = 180 64 216 504 >> mean(A) ans = 4.7500 3.5000 5.2500 5.0000 >> % Function 'max' works along rows... >> max(A) ans = 9 8 9 7 >> % ... or element by element... >> max(A,5) ans = 5 5 5 5 5 5 5 7 9 8 9 5 5 5 8 6 >> % ... or along columns... >> max(A,[],2) ans = 4 7 9 8 >> % Function 'diag' will extract the diagonal elements... >> diag(A) ans = 1 2 9 6 >> % Function 'det' will compute the determinate... >> det(A) ans = -824 >> % A host of trig functions are included (sin, cos, tan, etc.) >> sin(A) ans = 0.8415 0.9093 0.1411 -0.7568 -0.7568 0.9093 0.8415 0.6570 0.4121 0.9894 0.4121 0.1411 -0.9589 0.9093 0.9894 -0.2794 >> % A host of exponential functions are included (exp, log, log10, sqrt, etc.) >> log(A) ans = 0 0.6931 1.0986 1.3863 1.3863 0.6931 0 1.9459 2.1972 2.0794 2.1972 1.0986 1.6094 0.6931 2.0794 1.7918 >> % Several functions for complex numbers are available (abs, phase, imag, real, etc.) >> phase(C) ans = 1.1071 0.9828 0.9273 0.2450 0.4636 0.4636 1.4289 0.5191 0.7266 0.8442 0.3218 1.2490 1.5708 0 1.5708 0 >> % Rounding and remainder functions are available (floor, ceil, round, mod, etc.) >> mod(A,3) ans = 1 2 0 1 1 2 1 1 0 2 0 0 2 2 2 0 >> % Use 'help elfun' for help on elementary functions. >> % Use 'help datafun' for help on convolution and various discrete Fourier transform functions. >> % Use 'help' and 'lookfor' for more general help. >> % Complete documentation available online at http://www.mathworks.com/support/ >> % SCRIPTS AND FUNCTIONS >> % Scripts execute as if typed from the command line - same context, no parameters, no return values. >> % Functions execute in a separate context, accept parameters, and (optionally) return values. >> % Use Matlab's command 'edit' or your favorite text editor to create & use the extension '.m'. >> edit >> % Insert comments with the '%' character. Comment lines beginning in the first line of a script file or >> % immediately after the function declaration are presented when you request 'help' for the m-file. >> % Function files begin with the syntax: >> % function [ret1, ret2, ....] = myfun(param1,param2,....) % Parameters are passed by reference unless they are modified within the function. % The type command will show you what is inside a '.m' file... >> type myfun function [r_sum,r_diff] = myfun(p_a,p_b) % % This function returns the sum and difference % between the two parameters. r_sum = p_a + p_b; r_diff = p_a - p_b; >> help myfun This function returns the sum and difference between the two parameters. >> % Invoke a function as you would expect... >> [thesum,thediff] = myfun(A,B) thesum = 2 6 12 9 6 4 9 9 12 9 18 11 9 9 11 12 thediff = 0 -2 -6 -1 2 0 -7 5 6 7 0 -5 1 -5 5 0 >> % Scripts and functions must be in the working directory or on the Matlab search path. >> % Use 'pwd' to see your working directory and 'cd' to change it. Function 'dir' does what you'd expect... >> pwd ans = h:\dos >> % Use 'path' to see the search path, 'addpath' to add directories, and 'rmpath' to remove directories. >> % CONDITIONAL EXECUTION >> % Some operators return logical arrays... >> A>5 ans = 0 0 0 0 0 0 0 1 1 1 1 0 0 0 1 1 >> % These can be used like ordinary arrays... >> sum(sum(A>5)) ans = 6 >> % Bit-wise operators do what you would expect... >> (A>5)&(A<8) ans = 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 >> % Other bit-wise operators are | (or) and ~ (not). >> % Construct 'if expr, statement1, else, statement2, end' executes statement 1 if all elements of expr are >> % non-zero, otherwise statement2 is executed... >> if (A>5) disp('All elements are greater than five'); else disp('Some elements are not greater than five'); end Some elements are not greater than five >> % Construct 'for var=expr, statement1, end' assigns columns of expr to var in turn... >> for v=1:5 disp(v); end 1 2 3 4 5 >> % Also available are 'while' and 'switch' constructs. >> % See 'help lang' for more language constructs. >> % CHARACTER AND STRING MANIPULATION >> % Strings are row vectors of characters and can be denoted by ' '. >> mystr='Hello, Dave.' mystr = Hello, Dave. >> % They can be concatenated, subscripted, etc. just like other arrays. >> [mystr, ' How are you?'] ans = Hello, Dave. How are you? >> mystr(8) ans = D >> % The function 'num2str' will convert numeric arrays to text arrays formatted nicely... >> mystr=num2str(A) mystr = 1 2 3 4 4 2 1 7 9 8 9 3 5 2 8 6 >> % Function 'disp' will display any array on the command window. Function 'sprintf' will allow more careful >> % formatting and works very much as in C. >> % For more on strings, see 'help strings' or 'help strfun'. >> % FIGURES AND PLOTTING >> % Create a figure window with 'figure'... >> myfig=figure; >> myfig2=figure; >> % Activate a figure window with 'figure'... >> figure(myfig) >> % Close a figure window with 'close'... >> close(myfig2) >> % Make a plot in the current figure window with 'plot'... >> theta=0:0.2:2*pi; >> plot(theta,sin(theta)) >> % Simply plotting again will replace the old plot... >> plot(theta,cos(theta),'g') >> % To overlay a plot, use 'hold on'. To replace again, use 'hold off'... >> hold on >> plot(theta,sin(theta),'r') >> % We can make the plot more pleasing by removing the blank space on the right... >> axis([0,2*pi,-1,1]) >> % One should ALWAYS carefully label a plot... >> title('Sine and Cosine values','FontSize',14) >> legend(char({'Cosine','Sine'})) >> xlabel('Angle in Radians','FontSize',12) >> ylabel('Function Value','FontSize',12) >> % We can put more than one plot on a figure with 'subplot'. >> % Here we make a polar plot... >> myfig2=figure; >> subplot(2,1,1); >> title('Advanced Plotting') >> polar(theta,1-sin(theta)); >> xlabel('Polar Plot Example') >> % ...and a stem plot... >> subplot(2,1,2); >> stem(theta,cos(theta)) >> xlabel('Index') >> ylabel('F[n]') >> % From the file menu of the figure, you can print to paper or export to a variety of file types. >> %ADVANCED TOPICS >> % Matlab provides file I/O functions similar to C - see 'help iofun'. >> % Matlab provides support for multi-dimensional arrays >> % Matlab provides structured variables and objects similar to C - see 'help datatypes'. >> % Matlab provides cell arrays which are arrays of other arrays - see 'help datatypes'. >> diary off