%% Basic Matlab Commands % % *Wavelets Workshop* % % June 2 - 5, 2009 % % University of South Florida % % *Catherine Beneteau*, *Caroline Haddad*, *David Ruch*, *Patrick Van % Fleet* % To see the commands and the results in the same window, under File on the % main menu, select Save and Publish to HTML. % You can run this m-file - go to the command window and type % echodemo BasicCommands. % To evaluate individual cells, one at a time, place cursor in cell you % wish to evaluate, and either type Ctrl-Enter or under Cell on the top % menu, select Evaluate Current Cell. %% Creating Vectors v=[1 2 3 4] v=[1 2 3 4]' v=[1;2;3;4] A=[1 2; 3 4; 5 6] %% Individual Elements v=[1 2 3 4] v(1) v(4) [m]=size(v) v(1,m) A=[1 2; 3 4; 5 6] A(1,1) A(3,1) A(2:3,2) %extracts 2nd and 3elements of 2nd column of A A(2,:) % extracts 2nd row of A %% Format x=sqrt(2) format short x format long x format hex x format rat %% Dimension and size and matrix products A=[1 2 3;4 5 6] size(A) [m,n]=size(A) % m = #rows, n = # columns numel(A) B=[1 2; 3 4;-1 -2] A*B % product of two matrices B*A C=[-1 2;0 3;5 8] C.*B % multiplies corresponding components of A and B %% Length and norm and dot product v=[1 2 3 4] length(v) max(size(v)) norm(v) norm(v,2) norm(v,1) norm(v,inf) x=rand(1,10) y=rand(1,10) x'*y % dot product between 2randomly generated vectors x and y %% Reshape A=[ 1 2; 3 4 ; 5 6] n=numel(A) % number of elements FlattenByRowsA=reshape(A',1,n) FlattenByColsA=reshape(A,1,n) %% Reverse v=[1 2 3 4] reverse=x(end:-1:1) %% Sort v=[1 5 7 2 -9] sort(v) sort(v,'ascend') sort(v,'descend') A=[1 -2 7; 5 2 4] sort(A) sort(A,1) sort(A,2) %% Sum and cumsum, max and min of a matrix v sum(v) A sum(A) v cumsum(v) A cumsum(A) max(max(A)) min(min(A)) %% Listing Matlabs pre-prorammed functions using Matlab help % Under MATLAB--> FUNCTIONS by alphabetical list will % list the standard math fuctions, like exp(x), log(x),sin(x), which also % work or vectors! % % Doing the same in the Discrete Wavelets Toolbox will give a list of all % functions there and clicking on one will explain what it does, and % generally give an example. %% Common functions: abs, sqrt, sin, cos, exp, log abs(-3) sqrt(4) sin(pi/3) cos(pi/3) exp(pi*i) log(2.718) log2(256) log10(1000) %% sprint and disp disp('This is how to print out a string') sprintf('%s','hello') % %s is for string %% For s=0 for i =1:.5:5 % this executes the following command for i = 1,1.5,2,2.5,3,3.5,4,4.5,5 (1 to 5 using an increment of .5) s=s+i^2; end sprintf('s is %d',s) %d is for integer %% Plot and fplot for i = 1:10 x(i)=1+(i-1)*.1; y(i)=1/x(i)^2; end x y figure;plot(x,y,'g-');title('y=1/x^2') clear x y x=linspace(1,2,10) y=1./x.^2 figure;plot(x,y,'ro');title('y=1/x^2') clear x, y; figure; fplot(@(x)[abs(1/2+(1/2)*exp(i*x))],[-pi, pi]);title('|H(w)| with h=(1/2,1/2)'); %% Matlab allows you to easily create special commomly used matrices % eye(n) is the nxn identity matrix % ones(m,n) is the mxn ones matrix % zero(m,n) is the mxn zero matrix % diag(v) creates a square matrix with the entries of vector v on the diagonal % diag(A) creates a vector with the entries from the diagonal of matrix A % size(A) gives the size of the matrix % blkdiag(A, B, ...) creates a block diagonal matrix % norm(v) is the 2 norm, norm(v,1) is the 1-norm, norm(v,inf) is the infinity norm % norm(A) will do the induced matrix norms %% Special Matrices eye(5) zeros(3,5) ones(4,2) v=6*ones(1,6) A=diag(v,1)+diag([v/2 1])+diag(v/4,-1) diag(A) %% Useful Image commands % Note: Do not execute these without entering an actual image file name % Type is NOT required to be jpeg A=imread('Photoname.jpg') % Reads in a jpeg figure % This will open up a new figure image('image matrix name goes here') % displays the image title('Title goes here') axis('for example, square will make the image square') Gray=.2989*A(:,:,1)+.5870*A(:,:,2)+.1140*A(:,:,3); % This converts a color image to grayscale % To view it: figure,colormap(gray(256)),image(Gray), axis('square'),title('Title')