Useful MATLAB miscellany
This page contains a variety of MATLAB trivia/commands which are likely to be useful to members of the Experimental Neuroimaging Group but may also be of use to other people. There's no real formal structure to the page, just commands that have come up amongst us and may be useful for others.
Manipulating each row in a matrix using a different value
Consider these two matrices:
>> a = [1 2 3 4 5; 6 7 8 9 10] a = 1 2 3 4 5 6 7 8 9 10 >> b = [1; 2] b = 1 2 >> c = a*b
What I want is to multiply each value in the first row of a by the first value in b. The logical approach may be to try a simple multiplication. Unfortunately, MATLAB doesn't follow:
>> c = a*b ??? Error using ==> mtimes Inner matrix dimensions must agree.
This could be solved using a short loop but a better approach is to use bsxfun which is an element by element array manipulation tool:
>> c = bsxfun(@times,a,b) c = 1 2 3 4 5 12 14 16 18 20
This gives the desired result and is very useful for a variety of biological functions e.g. multiplying a series of datasets by individual scaling factors based on a standard. Many functions can be used here as well e.g. @plus, @minus and so on.
Extracting parts of a string
Although there is a substr function in MATLAB, like other programming languages, it often fails to work when you pass it data that's not as it likes. Instead, you can take advantage of the fact that MATLAB treats a string as an array of characters and use standard MATLAB notation to return portions of your string:
>> string = 'Some data'; >> s1 = string(1:3); % First 3 characters (s1 = 'Som') >> s2 = string(1:3:end); % Every 3rd character (s2 = 'Sea') >> s3 = string((length(string)-2:length(string)); % Last 3 characters (s3 = 'ata') >> s4 = string(end:-1:1); >> s5 = fliplr(string); % Reverse order (s4 = s5 = 'atad emoS')
Functions vs scriptshttp://web.cecs.pdx.edu/~gerry/MATLAB/programming/basics.html
Miscellaneous useful functions
It's frustrating when working with scripts on a network share and MATLAB doesn't recognise that they've changed when you save them. It is doubly frustrating when you are running a local cluster and each instance of MATLAB refuses to recognise the change. These two problems are solved in one command:
>> pctRunOnAll rehash path
The first part (pctRunOnAll) allows the following commmand to be run on all instances of MATLAB running on the host PC at that time. The latter part of the command (rehash path) refreshes MATLAB's cache of files by forcing a check of which files have changed throughout the path. This version of the command is preferred to the plain "rehash" when MATLAB has issued warnings during startup about not being notified of changes on a network share. For my part, I always run the "rehash path" version when working on a remote server.
>> a = [1 2 3; 4 5 6] a = 1 2 3 4 5 6 >> b = size (a) b = 2 3 >> b(2) ans = 3 % size of second dimension >> c = size (a,2) c = 3 % Alternative way to get size of second dimension
Removing data (rows or columns) from a matrix
The most efficient way to remove data is using this notation. The output in this case is an array 97x100 in size.
>> matrix = magic(100); >> matrix([1,4,50],:)=; %Removes rows 1, 4 and 50
Alternative methods are listed here.
- MATLAB tips and tricks. A useful site with a wide variety of pdf help files covering topics such as code optimisation, basic programming strategy and array manipulation.
First published on 26th November 2012 and last modified on 30th November 2012.