% % A few Matlab examples that can be cut and pasted directly into matlab % % (c) Markus Weber % % % Starting matlab % matlab % Type this in Unix % % Entering matrices % A = [ 1, 2, 3, ]; % produces a row vector B = [ 1; 2; 3 ]; % produces a column vector C = [ 1 2 3 ]'; % commas are redundant, "'" - operator gives transpose D = [ 1 2; 3 4; 5 6] % if you drop the ; at the end of a statement, the result is printed D' 1:10 % a:b is a short form for [ a, a + 1, ..., b ] [1:0.1:3]' % a:i:b allows to specify an increment % Some special building functions: zeros(2, 3) % Zeros ones(4, 2) % Ones eye(5) % Unit matrix diag([1 5 2]) % Diagonal matrix rand(10,2) % Random numbers % % Indexing matrix elements % D(3, 2) % single elements are obtained in the obvious way D(:, 2) % colon gives an entire column... D(1, :) % or row D = magic(10) % magic() just produces some data (a magic square actually...) D(2:4, 6:9) % vectors can be used to index sub-matrices D([1 3 5], :) % they do not need to specify continuous elements A = [0 0 0; 0 0 0; 0 0 0] A([1 3], :) = [5 6 7; 8 9 10] % You can select elements on the left hand side of an % assignment. Beware: the shape on the right hand side % has to fit! A([1 3], :) = [5 6 7; 8 9 10]' % This will not work! % % Matrix operations % % The intuitive things will work for scalars as well as for matrices A = [1 2 3]; B = [1 0 -1]; A + B A - B A + 100 % Scalars are sometimes expanded to the right size matrix a = 20; b = 1.5; a * b % Scalar multiplication C = magic(3) C * A' % For vector multiplication, the sizes obviously should match C * A % Bad... [1 2 3]' * [4 5 6] % Of course we can do outer products. [1 2 3] .* [10 100 1000] % Use the "." to specify pointwise operations [1:10] .^ 2 % It works... % % Elementary Math Functions % help elfun % Your favourite is there X = [-6:0.1:6]'; Y = [sin(X), 2 * cos(X), 3 * abs(cos(X))]; plot(Y); % % Plotting and Printing % figure; figure; % You can use many "figures" at a time... figure(2) % ... and select any one for output X = 1:10; Y = X .^ 2; plot(X, Y); % "plot" can plot 2-D data in many convenient ways plot(X, Y, 'y+'); % Uses yellow "+"s as markers help plot % Should give you a good idea hold on; % This way, your plot will not be erased... plot(X, 100 * rand(10, 1)) % ...and you can superimpose things... plot(X, 100 * rand(10, 1)) % ...and you can superimpose things plot(X, 100 * rand(10, 1)) % ...and you can superimpose things hold off; % enough % Some professors like to understand your plots: title('L''art pour l''art!!'); xlabel('Age'); ylabel('Activity in V1'); print -deps filename.eps % Creates a PostScript file that can then be printed % like this (under Unix): "lpr filenam.eps" clf; % Clears the plot % % Loading and Saving % A = magic(20); % Create important data B = magic(3); save huge_magic_square A % Save it as *.mat file. save -ascii tiny_magic_square B % Save it as ASCII file. clear; load huge_magic_square % Load it... whos % yes, it's back! load tiny_magic_square whos % % Images % more on; % For slow readers help images % Gives an overview of the image toolbox A = rand(100, 100); % Images are matrices imagesc(A); % Use imagesc() to display an image colormap gray; % You will mostly work on graylevel images help imread % See help pages for file handling of different image formats imread(clown) % % Filtering % Gauss1D = [1 4 6 4 1]; % An approximation of a 1D Gaussian Gauss1D = Gauss1D / sum(Gauss1D); % Normalizes it Gauss2D = Gauss1D' * Gauss1D; % Makes a two-dimensional version Gauss2D = Gauss2D / sum(sum(Gauss2D)); imagesc(conv2(A, Gauss2D, 'same')); % 2D convolution, make sure the result has same size % as input size(conv2(A, Gauss2D)) % See how it grows? % % Writing Progams (M-Files) % % % Some useful functions (you might have to get them from the website) % downsample2() % Implements a smart way to create lower resolution images [Xg Yg] = meshgrid(1:5, 1:5) % Generates X-Y grid