MATLAB Programming Tricks



Similar documents
CUDAMat: a CUDA-based matrix class for Python

Basic Concepts in Matlab

Scientific Programming in Python

Introduction to Matlab

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

CS1112 Spring 2014 Project 4. Objectives. 3 Pixelation for Identity Protection. due Thursday, 3/27, at 11pm

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

Programming Exercise 3: Multi-class Classification and Neural Networks

14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06

An Introduction to Applied Mathematics: An Iterative Process

CRASH COURSE PYTHON. Het begint met een idee

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

Fast Arithmetic Coding (FastAC) Implementations

Engineering Problem Solving and Excel. EGN 1006 Introduction to Engineering

Tutorial Program. 1. Basics

Introduction to MATLAB (Basics) Reference from: Azernikov Sergei

CHAPTER 1 Splines and B-splines an Introduction

Beginner s Matlab Tutorial

(!' ) "' # "*# "!(!' +,

Operation Count; Numerical Linear Algebra

Chapter 19. General Matrices. An n m matrix is an array. a 11 a 12 a 1m a 21 a 22 a 2m A = a n1 a n2 a nm. The matrix A has n row vectors

Introduction to the data.table package in R

DATA ANALYSIS II. Matrix Algorithms

LAYOUT OF THE KEYBOARD

THE CERN/SL XDATAVIEWER: AN INTERACTIVE GRAPHICAL TOOL FOR DATA VISUALIZATION AND EDITING

u = [ 2 4 5] has one row with three components (a 3 v = [2 4 5] has three rows separated by semicolons (a 3 w = 2:5 generates the row vector w = [ 2 3

Linear Algebra Review. Vectors

:Introducing Star-P. The Open Platform for Parallel Application Development. Yoel Jacobsen E&M Computing LTD

VISUAL ALGEBRA FOR COLLEGE STUDENTS. Laurie J. Burton Western Oregon University

Vector Notation: AB represents the vector from point A to point B on a graph. The vector can be computed by B A.

CALIBRATION OF A ROBUST 2 DOF PATH MONITORING TOOL FOR INDUSTRIAL ROBOTS AND MACHINE TOOLS BASED ON PARALLEL KINEMATICS

Integer Factorization using the Quadratic Sieve

The Characteristic Polynomial

Data Mining: Algorithms and Applications Matrix Math Review

Correcting the Lateral Response Artifact in Radiochromic Film Images from Flatbed Scanners

Essential Mathematics for Computer Graphics fast

Server Load Prediction

MATLAB and Big Data: Illustrative Example

What s New in MATLAB and Simulink

Parallel Computing using MATLAB Distributed Compute Server ZORRO HPC

MATLAB Basics MATLAB numbers and numeric formats

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Bildverarbeitung und Mustererkennung Image Processing and Pattern Recognition

OpenACC 2.0 and the PGI Accelerator Compilers

Analysis of System Performance IN2072 Chapter M Matlab Tutorial

CS 4204 Computer Graphics

The complete beginners guide to Adobe Illustrator. Get started

Part VI. Scientific Computing in Python

PGR Computing Programming Skills

Cover. SEB SIMOTION Easy Basics. Collection of standardized SIMOTION basic functions. FAQ April Service & Support. Answers for industry.

Notes on Factoring. MA 206 Kurt Bryan

General Framework for an Iterative Solution of Ax b. Jacobi s Method

Discrete Convolution and the Discrete Fourier Transform

2+2 Just type and press enter and the answer comes up ans = 4

3. Interpolation. Closing the Gaps of Discretization... Beyond Polynomials

Streamline your large format printing and finishing workflow

MATLAB DFS. Interface Library. User Guide

Vector Spaces; the Space R n

Question 2: How do you solve a matrix equation using the matrix inverse?

[1] Diagonal factorization

4. Right click grid ( ) and select New MODFLOW, then select Packages, enabling both the optional Well and Recharge option.

Continued Fractions and the Euclidean Algorithm

Multiplying Binomials and Factoring Trinomials Using Algebra Tiles and Generic Rectangles

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

Programming Languages & Tools

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0

WEEK #3, Lecture 1: Sparse Systems, MATLAB Graphics

Chapter 2. Point transformation. Look up Table (LUT) Fundamentals of Image processing

Kenken For Teachers. Tom Davis June 27, Abstract

Lecture 2 Matrix Operations

QUICK REFERENCE: ADOBE ILLUSTRATOR CS2 AND CS3 SECTION 1: CS3 TOOL BOX: PAGE 2 SECTION 2: CS2 TOOL BOX: PAGE 11

Introduction. Chapter 1

Fast Analytics on Big Data with H20

Numerical Methods I Eigenvalue Problems

CATIA V5 Tutorials. Mechanism Design & Animation. Release 18. Nader G. Zamani. University of Windsor. Jonathan M. Weaver. University of Detroit Mercy

Solution of Linear Systems

Nonlinear Iterative Partial Least Squares Method

KaleidaGraph Quick Start Guide

Introduction to MATLAB IAP 2008

Approximation Algorithms

Lecture 5: Singular Value Decomposition SVD (1)

x = + x 2 + x

Computational Mathematics with Python

Financial Reporting Using Microsoft Excel. Presented By: Jim Lee

CS3220 Lecture Notes: QR factorization and orthogonal transformations

2: Computer Performance

Data Structures. Algorithm Performance and Big O Analysis

How long is the vector? >> length(x) >> d=size(x) % What are the entries in the matrix d?

WEBFOCUS QUICK DATA FOR EXCEL

ECE 842 Report Implementation of Elliptic Curve Cryptography

Data Mining. Cluster Analysis: Advanced Concepts and Algorithms

RAID Basics Training Guide

Orthogonal Projections

Transcription:

MATLAB Programming Tricks Retreat 2015 Bad Überkingen Dominic Mai

Why use MATLAB? Easy to write code - No type declarations needed - Memory management handled automatically - Structs for easy data keeping - Duck typing Built in nd-arrays - Matrix-Vector arithmetic, linear algebra Lots of stuff available - Toolboxes (40+) - Matlabcentral - Interfaces to many 3rd party tools written in other languages: ANN, libsvm, LBFGS, Caffe,LSA TR, opencv,... 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 2

Why use MATLAB? Easy to debug - Interpreted language with interactive workspace MEX C/C++ interface Profiler Easy to generate nice looking plots Well documented 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 3

Downsides? Not free Interpreted language - Function calls and loops are (sometimes) relatively slow Vectorization (getting rid of loops) - Often not straight forward - Probably needs more memory Passing arguments to functions: Call by value - It's actually copy on write - there are ways around ( mex, global) 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 4

Overview Array Memory Layout Image Transformations Random Stuff 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 5

ND Arrays: memory arrangement MATLAB supports nd-arrays of arbitrary dimensions - Need continuous chunk of memory - Stored column major 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 6

ND Arrays: creationism Nested loops %fill up with nested for loop c = 1; for i3 = 1:3 for i2 = 1:2 for i1 = 1:4 A(i1,i2,i3) = c; c=c+1; end end end 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 7

ND Array - creationism Linear element access %linear element access A = zeros([4,2,3]);%allocate & def. shape for i = 1:numel(A) A(i) = i; end 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 8

ND Array - creationism Concatenation p1 = [1 2 3 4; 5 6 7 8]'; p2 = [9:12; 13:16]'; p3 = [17 21;18 22;19 23;20 24]; A = cat(3,p1,p2,p3); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 9

ND Array - creationism reshape - Fills along fastest moving dimension clear A; A = reshape(1:24,[4,2,3]); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 10

Nd Array element access 500³ elements: sum up - Different nested loops - 3 dimensions: how many ways are there? acc = 0; t = tic; for i3 = 1:N for i2 = 1:N for i1 = 1:N acc = acc + A(i1,i2,i3); end end end d = toc(t); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 11

Nd Array element access Linear access t = tic; for i = 1:numel(A) acc= acc+a(i); end d = toc(t); Vectorized (i.e. no loops) %v1: on flattened array acc = 0; t = tic; acc= sum(a(:)); d = toc(t); %v2: every sum reduces dim by 1 t = tic; acc= sum(sum(sum(a))); d = toc(t); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 12

Nd Array element access Different nested loops, 500³ elements 0.06s 0.03s 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 13

Nd Array element access Number of adjacent elements in memory 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 14

Nd Array element access: eval Create parameterizable code with eval str= [ 't=tic;',... 'acc=0; ',... 'for i%i=1:n ',... 'for i%i=1:n ',... 'for i%i=1:n ',... 'acc=acc+a(i1,i2,i3); ',... 'end; ',... 'end; ',... 'end; ',... 'd = toc(t);']; 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 15

Nd Array element access: eval Create parameterizable code p = perms([1 2 3]); for i = 1:size(p,1) a = p(i,:); cmd = sprintf(str, a(1), a(2), a(3)); eval(cmd); end perms([1 2 3]) ans: 3 2 1 3 1 2 2 3 1 2 1 3 1 2 3 1 3 2 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 16

Nd Array element access: eval Create parameterizable code - But: does not support JIT acceleration! 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 17

JIT acceleration Accelerates code (I guess mostly loops) - Not well documented, can leed to strange behavior function test1 tic; a = 2; for i = 1:10000000 a = a*1.00000001; end toc; a = 1:2; function test2 tic; a = 2; for i = 1:10000000 a = a*1.00000001; end toc; c = 1:10; 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 18

JIT acceleration Accelerates code (I guess mostly loops) - Avoid reassignment function test1 tic; a = 2; for i = 1:10000000 a = a*1.00000001; end toc; a = 1:2; function test2 tic; a = 2; for i = 1:10000000 a = a*1.00000001; end toc; c = 1:10; 2.31s 0.06s ~40x faster! 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 19

Alternative to eval Useful for creating scripts - Uses JIT - Globally mounted home: ssh + screen matlabexec = 'matlab -nodesktop -nosplash -r "%s; quit force"'; cmd = sprintf(matlabexec, ml_cmd); system(cmd); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 20

Overview Array Memory Layout Image Transformations - Gray value - Coordinate Random Stuff 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 21

Vectorization: Matrix Mult Multiply all 3-vectors of a [MxNx3] with a [3x3] matrix - Example: colorspace transformation im = single(imread('cat_sir.jpg')); %size: [631,553,3] im = im / max(im(:)); %rgb to xyz M = [0.5767309 0.1855540 0.1881852; 0.2973769 0.6273491 0.0752741; 0.0270343 0.0706872 0.9911085]; %naive: for loops tic; for i1 = 1:size(im,1) for i2 = 1:size(im,2) im(i1,i2,:) = M*squeeze(im(i1,i2,:)); end end toc; 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 22

Vectorization: Matrix Mult Multiply all 3-vectors of a [MxNx3] with a [3x3] matrix - Use shiftdim & reshape to form a 2d matrix suitable for matrix multiplication: im = shiftdim(im,2); shape = size(im);%[3,631,553] %create a 2D matrix of col vectors im = reshape(im, 3,[]);%[3, 631*553] im= M*im; %reshape to original im = shiftdim(reshape(im, shape),1);%[631,553,3]... 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 23

Vectorization: Matrix Mult Multiply all 3-vectors of a [MxNx3] with a [3x3] matrix - equally fast: channel by channel 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 24

Vectorization: Matrix Mult Timings - For loops: 20.77 - Shiftdim & Reshape: 0.007 - Channel wise: 0.01 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 25

Overview Array Memory Layout Image Transformations - Gray value - Coordinate Random Stuff 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 26

Patch-extract: interpn, ndgrid (linear) Transformations to extract an image patch source target 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 27

Patch-extract: interpn, ndgrid (linear) Transformations to extract an image patch 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 28

Patch-extract: interpn, ndgrid (linear) Transformations to extract an image patch 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 29

Patch-extract: interpn, ndgrid (linear) Transformations to extract an image patch %to origin M1 = [ 1 0 -y; 0 1 -x; 0 0 1]; %origin to center of target M2 = [ 1 0 target_size(1)/2; 0 1 target_size(2)/2; 0 0 1]; 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 30

Patch-extract: interpn, ndgrid Ndgrid: create grid indices for arbitrary dimensions [X,Y] = ndgrid(1:3,1:4) X: Y: 1 1 1 1 2 2 2 2 3 3 3 3 1 2 3 4 1 2 3 4 1 2 3 4 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 31

Patch-extract: interpn, ndgrid Putting stuff together %get inverse transformation M = pinv(m2*m1); %create grid [X,Y] = ndgrid(1:target_size(1), 1:target_size(2)); %apply transformation XT = M(1,1)*X + M(1,2)*Y + M(1,3); YT = M(2,1)*X + M(2,2)*Y + M(2,3); %read out source with interpolation target = zeros([target_size, size(im,3)]); for ch = 1:size(im,3) target(:,:,ch) = interpn(im(:,:,ch), XT, YT, 'bicubic',0); end 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 32

Patch-extract: interpn, ndgrid Adding Rotations R = [ cos(alpha), -sin(alpha), 0; sin(alpha), cos(alpha), 0; 0, 0, 1]; M = pinv(m2*r*m1); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 33

Patch-extract: interpn, ndgrid Border treatment: mirroring Artifacts due to Screencapture / libre office %mirror? XT(XT < 1) = 2 - XT(XT < 1); YT(YT < 1) = 2 - YT(YT < 1); XT(XT > size(im,1)) = 2*size(im,1) - XT( XT > size(im,1)); YT(YT > size(im,2)) = 2*size(im,2) - YT( YT > size(im,2)); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 34

Patch-extract: interpn, ndgrid Adding wobble %wobble u = randn(3,3); v = randn(3,3); U = imresize(u,size(x),'bicubic'); V = imresize(v,size(x),'bicubic'); % put all together XT = M(1,1)*X + M(1,2)*Y + M(1,3) + wobble * U; YT = M(2,1)*X + M(2,2)*Y + M(2,3) + wobble * V; 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 35

Overview Array Memory Layout Image Transformations Random Stuff 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 36

Startup.m Automatically load config file on startup.bashrc: - MATLABPATH=$MATLABPATH:/path/to/startup.m/ Startup.m: %add all paths recursively that are rooted here if(~isdeployed) addpath(genpath('/home/maid/phd/trunk/matlab/')); else disp('in deployed mode.'); end %load standard- config file: generative model detection global config; s = '/home/maid/phd/trunk/matlab/config.mat'; load(s); fprintf('loaded global config from %s\n',s); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 37

Global: config config = CROSSVALIDATION: 1 CVALS: [1x12 double] servers: {'dacky' 'frieda' 'william'} numjobs: [15 15 10] verbose: 0 filelocation: '/home/maid/phd/trunk/matlab/config.mat' debug: 0 trainingbasefolder:'/pathto/somefolder/' recomputefeatures: 0 computefeatures: @hogcellsforvolume_mex vibezversion: @myvibez3 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 38

Distribute values to struct %declare anonymous functions cellexpand = @(x) x{:}; numexpand = @(x) cellexpand(num2cell(x)); clear udets; %init struct, all accepted = 0 [udets(1:1000).accepted] = deal(0); %distribute array to struct [udets.id] = numexpand(1:1000); udets(1): accepted: 0 id: 1 udets(2): accepted: 0 id: 2 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 39

Standard function arguments function res = tut_varargin(a1,a2, varargin) %std. values optargs = {eps, 10, @zeros}; %replace std. values optargs(1:length(varargin)) = varargin; %give nicer variable names [tol, its, init_func] = optargs{:}; 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 40

combvec & bsxfun Create corners of a cuboidal box shape = [120, 80, 90]'; corners = bsxfun(@times, shape,... combvec([1,0], [1,0], [1,0])) corners: 120 0 120 0 120 0 120 0 80 80 0 0 80 80 0 0 90 90 90 90 0 0 0 0 combvec([1,0], [1,0], [1,0]) ans: 1 0 1 0 1 0 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 0 0 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 41

cell argument lists Cell expansion using {:} - Generate arbitrary long list of arguments - Does not need to be of uniform type %cell-argument list c = repmat({[0 1]},1,3); %c: 1x3 cell res = combvec(c{:}); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 42

ismember Pick out a subset %pick out a subset: ismember [dets(1:100).ch] = numexpand(randi(10,1,100)); subset = dets(ismember([dets.ch], [1,4,5])); [subset.ch] ans: Columns 1 through 21 5 5 1 5 4 4 4 5 1 4 1 4 5 5 1 5 1 5 1 4 5 Columns 22 through 29 4 5 1 1 5 1 4 4 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 43

histc Count occurences %count occurences count = histc([dets.ch], unique([dets.ch])); unique([dets.ch]): count: 1 2 3 4 5 6 7 8 9 10 9 12 16 9 11 7 10 5 13 8 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 44

Local maxima With morphological operations se = ones([mindist_x, mindist_y]); res = imdilate(data, se); lmax = find(data == res); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 45

Find Dependencies of.m files [flist, plist] = matlab.codetools.requiredfilesandproducts('getinvfield2.m') flist': '/home/maid/phd/trunk/matlab/3rdparty/ann_mwrapper/annquery.m' '/home/maid/phd/trunk/matlab/3rdparty/ann_mwrapper/private/ann_ mex.mexa64' '/home/maid/phd/trunk/matlab/3rdparty/ann_mwrapper/private/seto pts.m' '/home/maid/phd/trunk/matlab/3rdparty/ann_mwrapper/private/sho wdoc.m' '/home/maid/phd/trunk/matlab/helpers/getinvfield2.m' plist(1): Name: 'MATLAB' Version: '8.3' ProductNumber: 1 plist(2): Name: 'Image Processing Toolbox' Version: '9.0' ProductNumber: 17 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 46

JAVA integration Write Excel tables - http://www.mathworks.com/matlabcentral/fileexchange/ 38591-xlwrite--generate-xls-x--files-without-excel-onmac-linux-win sheet1 = {'col1', 'col2', 'col3'; 4 5 6; 'asdf' 4 'fgf'}; sheet2 = {'col1', 'col2', 'col3'; 4 5 6; 'asdf' 4 'fgf'}; xlwrite('tut_demo.xls', sheet1, 'page1'); xlwrite('tut_demo.xls', sheet2, 'Loss'); 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 47

JAVA integration Transparent variable conversion Java.awt.robot robot = java.awt.robot; robot.mousemove(x,y); robot.keypress(asciinum); http://de.mathworks.com/matlabcentral/fileexchange/ 32971-dynamic-search-box/content/java_robot.m 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 48

Fin. 25.Aug. 2015 Dominic Mai Uni Freiburg Retreat Bad ÜberkingenPräsentationstitel 49