MATLAB: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
Line 30: Line 30:
; length: returns the maximum dimension of a matrix
; length: returns the maximum dimension of a matrix
; A':transposes a matrix
; A':transposes a matrix
; 1:1:100: Generates numbers upto 100 beginning from 1 in steps of 1.
; <nowiki>1:1:100</nowiki>: Generates numbers upto 100 beginning from 1 in steps of 1.
; linspace: linspace(a,b,n) generates n linearly spaced points between a and b
; linspace: linspace(a,b,n) generates n linearly spaced points between a and b
; logspace: generates a logpspace
; logspace: generates a logpspace
; A(:,4:end): : is a wildcard (similar to * in shell). This returns all rows of A and columns from 4 onwards. ''end'' is a keyword that tells MATLAB to take elements untill the last column is reached.
; <nowiki>:</nowiki>: : (colon) is a wildcard (similar to * in shell). e.g. A(:,4:end). This returns all rows of A and columns from 4 onwards. ''end'' is a keyword that tells MATLAB to take elements untill the last column is reached.
; A([1 3 5 7],[2 4 6 8]): any submatrix can be addressed; it can even be assigned a value. For example A([1 3 5 7],[2 4 6 8])=0 sets those elements to zero!
; A([1 3 5 7],[2 4 6 8]): any submatrix can be addressed; it can even be assigned a value. For example A([1 3 5 7],[2 4 6 8])=0 sets those elements to zero!
; prod(size(x)): a simple construct for calculating the number of elements in x.
; prod(size(x)): a simple construct for calculating the number of elements in x.
; find(A): returns the indices of the non-zero elements in a vector. For matrices, it is better to give two outputs: [i,j]=find(A).
; find(A): returns the indices of the non-zero elements in a vector. For matrices, it is better to give two outputs: [i,j]=find(A).
; sqrt(i): complex variables are handled seamlessly. i and j are both square roots of -1. Avoid i,j in loops, therefore.
; sqrt(i): complex variables are handled seamlessly. i and j are both square roots of -1. Avoid i,j in loops, therefore.
; inline: Can create functions like <math>f(x,y)=\sin(x^2+y^2+xy)</math>
; inline: Can create functions like <math>f(x,y)=\sin(x^2+y^2+xy)</math>. e.g.:
    >> f=inline('sin(x.^2+y.^2+x.*y)')
   
    f =
   
        Inline function:
        f(x,y) = sin(x.^2+y.^2+x.*y)
; sparse: Use whenever memory problems are possible due to the usage of large matrices. MATLAB intelligently applies the corresponding algorithms. Typically, just saying A=sparse(A) may cause your code to run 10 times faster, if your matrix is large and sparse.
; sparse: Use whenever memory problems are possible due to the usage of large matrices. MATLAB intelligently applies the corresponding algorithms. Typically, just saying A=sparse(A) may cause your code to run 10 times faster, if your matrix is large and sparse.



Revision as of 01:49, 15 February 2006

About MATLAB

MATLAB is a numerical computing environment and programming language. Created by The MathWorks, MATLAB allows easy matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. Although it specializes in numerical computing, an optional toolbox interfaces with the Maple symbolic engine, making it a full computer algebra system. It is used by more than one million people in industry and academia and runs on most modern operating systems, including Windows, Mac OS, Linux and Unix. The current version is MATLAB 7.1 Service Pack 3. It is available for commercial use for approximately US$2000 and US$100 for an academic license with a limited set of Toolboxes.
— from Wikipedia:MATLAB

For Bioinformatics and Systems Biology, there are useful toolboxes available:

Elementary Tutorial

The following is an unstructured quick introduction to some of MATLAB's important commands/functions. MATLAB is reasonably easy to use and intuitive to pick up, so go ahead and experiment and learn!

GENERAL COMMANDS

clc
clears the command window
clear
removes all variables from the workspace
clear all
removes all variables, globals, functions and MEX links
ls
Returns listing of the current directory (works on Windows as well)
!ls
Use ! to run shell commands both in windows and linux. In windows, !explorer . maybe a useful command, to browse the current directory
pwd
Shows the present working directory (in Windows too)
help <command name>
gives a brief documentation
doc <command name>
gives the complete documentation
lookfor <text>
looks for the string <text> n the first comment line of the HELP text in all M-files (MATLAB functions/scripts) found on MATLABPATH.
whos
It lists all the variables in the current workspace, together with information about their size, bytes, class, etc.
whos -file <filename.mat>
lists the variables in the specified .MAT file.
type <filename>
'cats' or echoes the file to the screen


ESSENTIAL FUNCTIONS

size
returns the size of the matrix
length
returns the maximum dimension of a matrix
A'
transposes a matrix
1:1:100
Generates numbers upto 100 beginning from 1 in steps of 1.
linspace
linspace(a,b,n) generates n linearly spaced points between a and b
logspace
generates a logpspace
:
: (colon) is a wildcard (similar to * in shell). e.g. A(:,4:end). This returns all rows of A and columns from 4 onwards. end is a keyword that tells MATLAB to take elements untill the last column is reached.
A([1 3 5 7],[2 4 6 8])
any submatrix can be addressed; it can even be assigned a value. For example A([1 3 5 7],[2 4 6 8])=0 sets those elements to zero!
prod(size(x))
a simple construct for calculating the number of elements in x.
find(A)
returns the indices of the non-zero elements in a vector. For matrices, it is better to give two outputs: [i,j]=find(A).
sqrt(i)
complex variables are handled seamlessly. i and j are both square roots of -1. Avoid i,j in loops, therefore.
inline
Can create functions like [math]\displaystyle{ f(x,y)=\sin(x^2+y^2+xy) }[/math]. e.g.:
   >> f=inline('sin(x.^2+y.^2+x.*y)')
   
   f =
   
        Inline function:
        f(x,y) = sin(x.^2+y.^2+x.*y)
sparse
Use whenever memory problems are possible due to the usage of large matrices. MATLAB intelligently applies the corresponding algorithms. Typically, just saying A=sparse(A) may cause your code to run 10 times faster, if your matrix is large and sparse.

SPECIAL MATRICES

ones
zeros
rand
eye
diag

OUTPUT RELATED

x=1 vs x=1;
disp
sprintf
fprintf
load
importdata
xlsread
save (fname or matlab.mat)
format long
format rat

MATRIX FUNCTIONS

inv
inv vs \
* (multiplication)
.*
norm
expm(A) vs exp(A)
A^5 vs A.^5
svd
eig


OTHER FUNCTIONS

polyval
compan

ADVANCED

cell
symbolic

FIGURE RELATED FUNCTIONS

clf
figure
close
close all
hold on
waitforbuttonpress
subplot
plot
ezplot
surf
mesh
title
colormap

PROGRAMMING

function name and file name
programs vs scripts
% comments
help and comments
if (x~=1)
elseif
end - ENDs all blocks
for k=1
100 (not i!!)
All addressing is from 1
n
handling arguments
for loops is bad programming... there should be vectorisable alternatives!!

BIOINFORMATICS TOOLBOX

clustergram

SBMLToolBox

~libSBML for MATLAB!