tips - Matlab Tips and Tricks Gabriel Peyr´ e...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Matlab Tips and Tricks Gabriel Peyr´ e [email protected] August 10, 2004 First keep in mind that this is not a Matlab tutorial. This is just a list of tricks I have found useful while writing my toolboxes available on the Matlab Central repository http://www.mathworks.com/matlabcentral/ You can e-mail me if you have corrections about these pieces of code, or if you would like to add your own tips to those described in this document. Contents 1 General Programming Tips 1 2 General Mathematical Tips 4 3 Advanced Mathematical Tips 4 4 Signal and Image Processing Tips 6 5 Graph Theory Tips 10 1 General Programming Tips • Suppress entries in a vector. x( 3:5 ) = ; • Reverse a vector. x = x(end:-1:1); • Compute the running time of a function call. 1 tic; fft(rand(500)); disp( [’it takes ’ num2str(toc) ’s.’] ); • Make a array full of a % guess which one is the fastest ? tic; NaN*ones(2000,2000); toc; tic; repmat(NaN,2000,2000); toc; • Turn an nD array into a vector. x = x(:); • Compute the maximum value of an nD array. m = max(x(:)); • Access a matrix from a list of entries. Here, we have I = [I1; I2] and y(i) = M( I1(i), I2(i) ) J = sub2ind(size(M), I(1,:),I(2,:) ); y = M(J); • Create a function that take optional arguments in a struct. function y = f(x,options) % parse the struct if nargin<2 options.null = 0; % force creation of options end if isfield(options, ’a’) options.a = 1; % default value end a = options.a; if isfield(options, ’b’) options.b = 1; % default value end b = options.b; % Here the body of the function ... • Create a graphical waitbar. n = 100; h = waitbar(0,’Waiting ...’); for i=1:n waitbar(i/n); % here perform some stuff end close(h); • How to duplicate a character n times. 2 str = char( zeros(n,1)+’*’ ); • Output a string without carriage return. fprintf(’Some Text’); • Assign value v in a nD array at a position ind (lenth- n vector). ind = num2cell(ind); x( ind { : } ) = v; • Save the current figure as an image in e.g. EPS file format. saveas(gcf, str, ’png’); • Remove the ticks from a drawing. set(gca, ’XTick’, ); set(gca, ’YTick’, ); • Saving and loading an image. saveas(gcf, ’my image’, ’png’); % save M = double( imread( ’my image.png’ ) ); % load • Saving and loading a matrix M in a binary file. [n,p] = size(M); % saving str = ’my file’; % name of the file fid = fopen(str,’wb’); if fid<0 error([’error writing to file ’, str]); end fwrite(fid,M,’double’); fclose(fid); % loading fid = fopen(str,’rb’); if fid<0 error([’error reading file ’,str]); end [M, cnt] = fread(fid,[n,p],’double’); fclose(fid);...
View Full Document

This note was uploaded on 01/14/2011 for the course MATH 571 taught by Professor Staff during the Winter '08 term at University of Michigan.

Page1 / 10

tips - Matlab Tips and Tricks Gabriel Peyr´ e...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online