Escolar Documentos
Profissional Documentos
Cultura Documentos
Contents
1 General Programming Tips 2 General Mathematical Tips 3 Advanced Mathematical Tips 4 Signal and Image Processing Tips 5 Graph Theory Tips 1 4 4 6 10
Reverse a vector.
x = x(end:-1:1);
);
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); );
Find the angle that makes a 2D vector x with the vector [1,0]
% just the angle theta = atan2(x(2),x(1)); % if you want to compute the full polar decomposition [theta,r] = cart2pol(x);
Compute J, the reverse of a permutation I, i.e. an array which contains the number 1:n in arbitrary order.
J(I) = 1:length(I);
Shue an array x.
y = x( randperm(length(x)) );
% tensor product gaussian is isotropic x = randn(3,n); d = sqrt( x(1,:).2+x(2,:).2+x(2,:).2 ); x(1,:) = x(1,:)./d; x(2,:) = x(2,:)./d; x(3,:) = x(3,:)./d;
Construct a polygon x whose ith sidelength is s(i). Here x(i) is the complex ax of the ith vertex.
theta = [0;cumsum(s)]; theta = theta/theta(end); theta = theta(1:(end-1)); x = exp(2i*pi*theta); L = abs(x(1)-x(2)); x = x*s(1)/L; % rescale the result
Compute the curvilinear abscise s of a curve c. Here, c(:,i) is the ith point of the curve.
D = c(:,2:end)-c(:,1:(end-1)); s = zeros(size(c,2),1); s(2:end) = sqrt( D(1,:).2 + D(2,:).2 ); s = cumsum(s);
% x % I
first solution = (abs(x)>=T) .* x; second one : nearly 2 times slower = find(abs(x)<T); x(I) = 0;
Keep only the n biggest coecients of a signal x (set the others to 0).
[,I] = sort(abs(x(:))); x( I(1:end-n) ) = 0;
Draw a 3D sphere.
p = 20; % precision t = 0:1/(p-1):1; [th,ph] = meshgrid( t*pi,t*2*pi ); x = cos(th); y = sin(th).*cos(ph); z = sin(th).*sin(ph); surf(x,y,z, z.*0); % some pretty rendering options shading interp; lighting gouraud; camlight infinite; axis square; axis off;
Perform a 1D convolution of signal f and lter h with symmetric boundary conditions. The center of the lter is 0 for odd length lter, and 1/2 otherwise
n = length(x); p = length(h); if mod(p,2)==1 d1 = (p-1)/2; d2 = (p-1)/2; else d1 = p/2-1; d2 = p/2; end xx = [ x(d1:-1:1); x; x(end:-1:end-d2+1) ]; y = conv(xx,h); y = y( (2*d1+1):(2*d1+n) );
Extract all 0th level curves from an image M an put these curves into a cell array c list.
c = contourc(M,[0,0]); k = 0; p = 1; while p < size(c, 2) % parse the result lc = c(2,p); % length of the curve cc = c(:,(p+1):(p+lc)); p = p+lc+1; k = k+1; c list{k} = cc; end
Perform spectral interpolation of a signal x (aka Fourier zero-padding). The original size is n and the nal size is p
n f f x = = = = length(x); n0 = (n-1)/2; fft(x); % forward transform p/n*[f(1:n0+1); zeros(p-n,1); f(n0+2:n)]; real( ifft(f) ); % backward transform
Compute the approximation error err= ||f fM ||/||f || obtained when keeping the M best coecients in an orthogonal basis.
% as an example we take the decomposition in the cosine basis M = 500; x = peaks(128); y = dct(x); % a sample function [tmp,I] = sort(abs(y(:))); y(I(1:end-M)) = 0; err = norm(y,fro)/norm(x,fro); % the relative error xx = idct(y); imagesc(xx); % the reconstructed function
Perform a JPEG-like transform of an image x (replace dct by idct to compute the inverse transform).
bs = 8; % size of the blocks n = size(x,1); y = zeros(n,n); nb = n/bs; % n must be a multiple of bs for i=1:nb for j=1:nb xsel = ((i-1)*bs+1):(i*bs); ysel = ((j-1)*bs+1):(j*bs); y(xsel,ysel) = dct(x(xsel,ysel)); end end
Compute the shortest distance between all pair of nodes (D is the weighted adjacency matrix).
% non connected vectices must have Inf value N = length(D); for k=1:N D = min(D,repmat(D(:,k),[1 N])+repmat(D(k,:),[N 1])); end D1 = D;
10