I think everyone would agree that the MATLAB language is not pretty, or particularly consistent. But nevermind! We still have to use it to get things done.
What are your favourite tricks for making things easier? Let's have one per answer so people can vote them up if they agree. Also, try to illustrate your answer with an example.
-
Here's a quick example:
I find the comma separated list syntax quite useful for building function calls:
% Build a list of args, like so: args = {'a', 1, 'b', 2}; % Then expand this into arguments: output = func(args{:}) -
Invoking Java code from Matlab)
-
Turn a matrix into a vector using a single colon.
x = rand(4,4); x(:)Drazick : How would yo do it for a sub matrix? Let's say: x = rand(20, 20); I want to turn x(1:10, 1:10) into a vector. Are y=reshape(x(:10, 1:10), [], 1) or y=x(1:10, 1:10)-> y=y(:) my only options? Needless to say that x(1:10, 1:10)(:) won't work.Scottie T : @Drazick, you can access the elements of x using multiple dimensional indicies, or a single dimensional index. `myElems = [1:10 21:30 31:40...181:190]; y = x(myElems);`Drazick : Let's say I have an image - I. to calculate it variance I would do: var(I(:)). What if I want to calculate the variance of part of it - I(1:20, 1:20). var(var(I(1:20, 1:20)) won't do it (It's wrong). The options I know about, y = I(1:20, 1:20) -> var(y(:)) or y=reshape(I(1:20, 1:20), [], 1) -> var(y(:)). What I'm asking is there a way to apply the colon operator on sub matrices of a matrix without reallocating it? Thanks. -
Provide quick access to other function documentation by adding a "SEE ALSO" line to the help comments. First, you must include the name of the function in all caps as the first comment line. Do your usual comment header stuff, then put SEE ALSO with a comma separated list of other related functions.
function y = transmog(x) %TRANSMOG Transmogrifies a matrix X using reverse orthogonal eigenvectors % % Usage: % y = transmog(x) % % SEE ALSO % UNTRANSMOG, TRANSMOG2When you type "help transmog" at the command line, you will see all the comments in this comment header, with hyperlinks to the comment headers for the other functions listed.
-
cellfun and arrayfun for automated for loops.
-
The colon operator for the manipulation of arrays.
@ScottieT812, mentions one: flattening an array, but there's all the other variants of selecting bits of an array:
x=rand(10,10); flattened=x(:); Acolumn=x(:,10); Arow=x(10,:); y=rand(100); firstSix=y(1:6); lastSix=y(end-5:end); alternate=y(1:2:end);Jonas : lastSix = y(end-5:end); Your version returns 7 elements.Ian Hopkinson : Thanks @jonas - off by one, again! -
Using nargin to set default values for optional arguments and using nargout to set optional output arguments. Quick example
function hLine=myplot(x,y,plotColor,markerType) % set defaults for optional paramters if nargin<4, markerType='none'; end if nargin<3, plotColor='k'; end hL = plot(x,y,'linetype','-', ... 'color',plotColor, ... 'marker',markerType, ... 'markerFaceColor',plotColor,'markerEdgeColor',plotColor); % return handle of plot object if required if nargout>0, hLine = hL; end -
Directly extracting the elements of a matrix that satisfy a particular condition, using logical arrays:
x = rand(1,50) .* 100; xpart = x( x > 20 & x < 35);Now xpart contains only those elements of x which lie in the specified range.
devin : in matlab, you can use the function find to do basically the same thing.Marc : But find is MUCH slower. Logical Indexing is way faster, unless you need to know the indices of the matches. -
Using the built-in profiler to see where the hot parts of my code are:
profile on % some lines of code profile off profile vieweror just using the built in
ticandtocto get quick timings:tic; % some lines of code toc; -
Using ismember() to merge data organized by text identfiers. Useful when you are analyzing differing periods when entries, in my case company symbols, come and go.
%Merge B into A based on Text identifiers UniverseA = {'A','B','C','D'}; UniverseB = {'A','C','D'}; DataA = [20 40 60 80]; DataB = [30 50 70]; MergeData = NaN(length(UniverseA),2); MergeData(:,1) = DataA; [tf, loc] = ismember(UniverseA, UniverseB); MergeData(tf,2) = DataB(loc(tf)); MergeData = 20 30 40 NaN 60 50 80 70 -
Oh, and reverse an array
v = 1:10; v_reverse = v(length(v):-1:1);Matt : Hmm. I'd just use `flipud()` or `fliplr()` to do this. However, combined with steps, this is more useful. e.g. v(end:-4:1) for example.Revah : I like my way vs. flipud()/fliplr() because you don't have to know whether you have a column vector or a row vector.Jonas : You can drop the length() call and write v_reverse = v(end:-1:1); -
Vectorizing loops. There are lots of ways to do this, and it is entertaining to look for loops in your code and see how they can be vectorized. The performance is astonishingly faster with vector operations!
Matt : is this still the case now that Matlab has a JIT compiler? It would be interesting to see. -
Vectorization:
function iNeedle = findClosest(hay,needle) %FINDCLOSEST find the indicies of the closest elements in an array. % Given two vectors [A,B], findClosest will find the indicies of the values % in vector A closest to the values in vector B. [hay iOrgHay] = sort(hay(:)'); %#ok must have row vector % Use histogram to find indices of elements in hay closest to elements in % needle. The bins are centered on values in hay, with the edges on the % midpoint between elements. [iNeedle iNeedle] = histc(needle,[-inf hay+[diff(hay)/2 inf]]); %#ok % Reversing the sorting. iNeedle = iOrgHay(iNeedle); -
Here's a bunch of nonobvious functions that are useful from time to time:
mfilename(returns the name of the currently running MATLAB script)dbstack(gives you access to the names & line numbers of the matlab function stack)keyboard(stops execution and yields control to the debugging prompt; this is why there's a K in the debug promptK>>dbstop error(automatically puts you in debug mode stopped at the line that triggers an error)
-
LaTeX mode for formulas in graphs: In one of the recent releases (R2006?) you add the additional arguments
,'Interpreter','latex'at the end of a function call and it will use LaTeX rendering. Here's an example:t=(0:0.001:1)'; plot(t,sin(2*pi*[t t+0.25])); xlabel('t'); ylabel('$\hat{y}_k=sin 2\pi (t+{k \over 4})$','Interpreter','latex'); legend({'$\hat{y}_0$','$\hat{y}_1$'},'Interpreter','latex');Not sure when they added it, but it works with R2006b in the text(), title(), xlabel(), ylabel(), zlabel(), and even legend() functions. Just make sure the syntax you are using is not ambiguous (so with legend() you need to specify the strings as a cell array).
-
Executing a Simulink model directly from a script (rather than interactively) using the
simcommand. You can do things like take parameters from a workspace variable, and repeatedly runsimin a loop to simulate something while varying the parameter to see how the behavior changes, and graph the results with whatever graphical commands you like. Much easier than trying to do this interactively, and it gives you much more flexibility than the Simulink "oscilloscope" blocks when visualizing the results. (although you can't use it to see what's going on in realtime while the simulation is running)A really important thing to know is the
DstWorkspaceandSrcWorkspaceoptions of thesimsetcommand. These control where the "To Workspace" and "From Workspace" blocks get and put their results.Dstworkspacedefaults to the current workspace (e.g. if you callsimfrom inside a function the "To Workspace" blocks will show up as variables accessible from within that same function) butSrcWorkspacedefaults to the base workspace and if you want to encapsulate your call tosimyou'll want to setSrcWorkspacetocurrentso there is a clean interface to providing/retrieving simulation input parameters and outputs. For example:function Y=run_my_sim(t,input1,params) % runs "my_sim.mdl" % with a From Workspace block referencing I1 as an input signal % and parameters referenced as fields of the "params" structure % and output retrieved from a To Workspace block with name O1. opt = simset('SrcWorkspace','current','DstWorkspace','current'); I1 = struct('time',t,'signals',struct('values',input1,'dimensions',1)); Y = struct; Y.t = sim('my_sim',t,opt); Y.output1 = O1.signals.values; -
Contour plots with
[c,h]=contourandclabel(c,h,'fontsize',fontsize). I usually use thefontsizeparameter to reduce the font size so the numbers don't run into each other. This is great for viewing the value of 2-D functions without having to muck around with 3D graphs. -
Anonymous functions, for a few reasons:
- to make a quick function for one-off uses, like 3x^2+2x+7. (see listing below) This is useful for functions like
quadandfminbndthat take functions as arguments. It's also convenient in scripts (.m files that don't start with a function header) since unlike true functions you can't include subfunctions. - for closures -- although anonymous functions are a little limiting as there doesn't seem to be a way to have assignment within them to mutate state.
.
% quick functions f = @(x) 3*x.^2 + 2*x + 7; t = (0:0.001:1)'; plot(t,f(t),t,f(2*t),t,f(3*t); % closures (linfunc below is a function that returns a function, % and the outer function's arguments are held for the lifetime % of the returned function. linfunc = @(m,b) @(x) m*x+b; C2F = linfunc(9/5, 32); F2C = linfunc(5/9, -32*5/9); - to make a quick function for one-off uses, like 3x^2+2x+7. (see listing below) This is useful for functions like
-
conditional arguments in the left-hand side of an assignment:
t = (0:0.005:10)'; x = sin(2*pi*t); x(x>0.5 & t<5) = 0.5; % This limits all values of x to a maximum of 0.5, where t<5 plot(t,x); -
I like using function handles for lots of reasons. For one, they are the closest thing I've found in MATLAB to pointers, so you can create reference-like behavior for objects. There are a few neat (and simpler) things you can do with them, too. For example, replacing a switch statement:
switch number, case 1, outargs = fcn1(inargs); case 2, outargs = fcn2(inargs); ... end % %can be turned into % fcnArray = {@fcn1, @fcn2, ...}; outargs = fcnArray{number}(inargs);I just think little things like that are cool.
-
Know your axis properties! There are all sorts of things you can set to tweak the default plotting properties to do what you want:
set(gca,'fontsize',8,'linestyleorder','-','linewidth',0.3,'xtick',1:2:9);(as an example, sets the fontsize to 8pt, linestyles of all new lines to all be solid and their width 0.3pt, and the xtick points to be [1 3 5 7 9])
Line and figure properties are also useful, but I find myself using axis properties the most.
-
Matlab's bsxfun, arrayfun, cellfun, and structfun are quite interesting and often save a loop.
M = rand(1000, 1000); v = rand(1000, 1); c = bsxfun(@plus, M, v);This code, for instance, adds column-vector v to each column of matrix M.
Though, in performance critical parts of your application you should benchmark these functions versus the trivial for-loop because often loops are still faster.
-
Be strict with specifying dimensions when using aggregation functions like min, max, mean, diff, sum, any, all,...
For instance the line:
reldiff = diff(a) ./ a(1:end-1)might work well to compute relative differences of elements in a vector, however in case the vector degenerates to just one element the computation fails:
>> a=rand(1,7); >> diff(a) ./ a(1:end-1) ans = -0.5822 -0.9935 224.2015 0.2708 -0.3328 0.0458 >> a=1; >> diff(a) ./ a(1:end-1) ??? Error using ==> rdivide Matrix dimensions must agree.If you specify the correct dimensions to your functions, this line returns an empty 1-by-0 matrix, which is correct:
>> diff(a, [], 2) ./ a(1, 1:end-1) ans = Empty matrix: 1-by-0 >>The same goes for a min-function which usually computes minimums over columns on a matrix, until the matrix only consists of one row. - Then it will return the minimum over the row unless the dimension parameter states otherwise, and probably break your application.
I can almost guarantee you that consequently setting the dimensions of these aggregation functions will save you quite some debugging work later on.
At least that would have been the case for me. :)
-
I'm surprised that while people mentioned the logical array approach of indexing an array, nobody mentioned the find command.
e.g. if x is an NxMxO array
x(x>20) works by generating an NxMxO logical array and using it to index x (which can be bad if you have large arrays and are looking for a small subset
x(find(x>20)) works by generating list (i.e. 1xwhatever) of indices of x that satisfy x>20, and indexing x by it. "find" should be used more than it is, in my experience.
More what I would call 'tricks'
you can grow/append to arrays and cell arrays if you don't know the size you'll need, by using end + 1 (works with higher dimensions too, so long as the dimensions of the slice match -- so you'll have to initialize x to something other than [] in that case). Not good for numerics but for small dynamic lists of things (or cell arrays), e.g. parsing files.
e.g.
>> x=[1,2,3] x = 1 2 3 >> x(end+1)=4 x = 1 2 3 4
Another think many people don't know is that for works on any dim 1 array, so to continue the example
>> for n = x;disp(n);end 1 2 3 4Which means if all you need is the members of x you don't need to index them.
This also works with cell arrays but it's a bit annoying because as it walks them the element is still wrapped in a cell:
>> for el = {1,2,3,4};disp(el);end [1] [2] [3] [4]So to get at the elements you have to subscript them
>> for el = {1,2,3,4};disp(el{1});end 1 2 3 4I can't remember if there is a nicer way around that.
-
-You can make a Matlab shortcut to an initialization file called startup.m. Here, I define formatting, precision of the output, and plot parameters for my Matlab session (for example, I use a larger plot axis/font size so that .fig's can be seen plainly when I put them in presentations.) See a good blog post from one of the developers about it http://blogs.mathworks.com/loren/2009/03/03/whats-in-your-startupm/ .
-You can load an entire numerical ascii file using the "load" function. This isn't particularly fast, but gets the job done quickly for prototyping (shouldn't that be the Matlab motto?)
-As mentioned, the colon operator and vectorization are lifesavers. Screw loops.
-
Using xlim and ylim to draw vertical and horizontal lines. Examples:
Draw a horizontal line at y=10:
line(xlim [10 10])Draw vertical line at x=5:
line([5 5] ylim)
-
In order to be able to quickly test a function, I use
narginlike so:function result = multiply(a, b) if nargin == 0 %no inputs provided, run using defaults for a and b clc; disp('RUNNING IN TEST MODE') a = 1; b = 2; end result = a*b;Later on, I add a unit test script to test the function for different input conditions.
0 comments:
Post a Comment