<<

MATLAB/GNU Octave quick reference sheet A = zeros([,M]) Create a N × M with Plotting and visualization Last revision: February 21, 2013 values 0 A = ones([,M]) Create a N × M matrix with In the following, n and m can be single values or vectors. By Anders Damsgaard Christensen, values 1 figure Create new figure window Plot vector y as a of x [email protected], http://cs.au.dk/~adc. A = rand([,M]) Create a N × M matrix with plot(x,y) The < > symbols denote required arguments, [] args. are optional. uniformly distr. values in [0, 1[ with a line The bracketing symbols should not be written. A = randn([,M]) Create a N × M matrix plot(x,y,’*’) Plot vector y as a function of x with normal (Gaussian) distr. with points values with µ = 0, σ = 1 plot(x,y,’*-’) Plot vector y as a function of x with a line and points General session control semilogx(x,y) Plot vector y as a function of x, with x on a log scale whos List all defined variables semilogy(x,y) Plot vector y as a function of x, Arithmetic and standard functions clear Delete all defined variables with y on a log scale clc Clear home screen loglog(x,y) Plot vector y as a function of x, on edit [.m] edit file, create if it doesn’t In general, the number of elements returned equal the dimen- a loglog scale sions of the input variables. a*b = ab, a/b = a , a**b = ab, already exist √ b √ hist(x) Plot a histogram of values in x b save ’’ Save all variables to .mat = |a|, log(a,b) = logb(a) sin(a) = sin(a), background load ’’ Load variables from .mat M*N: multiplication of two vectors/matrices title(’bla’) Set a plot title Quick help on command A(:): show matrix as vector xlabel(’bla’) Set x-axis label doc Extensive help on command A’: of vector/matrix =[A;B]: Concentrate two vectors/matrices size(A): Dimensions of vector/matrix Custom functions sum(A): Column sum of vector/matrix elations: ==, ~=, >, <, <=, >= Variables inv(A): Inverse of matrix Conditional structures: det(A): of matrix When assigning variables, the values will be dis- if expr ...[elseif ...] [else ...] end A\b: For a matrix A and col. vector b find solution x to Ax = b played. This can be suppresed by adding the suffix Iteration structures: for var=expr ... end ; Constants: pi = π, e = e, i = i, inf = ∞ ans Value of last calculation Function syntax: x = 1 Define variable x to be a function [out1, ...] = name (par1, ...) with value 1 ... x = y Set x equal to y end v = [1,2,3] Define variable v to be a row  Vector/matrix slicing vector with values 1 2 3 v = [1;2;3] Define variable v to be a In the following, n and m can be single values or vectors. MATLAB reference manual column vector v() The n-th value of vector v http://www.mathworks.se/help/matlab/index.html M = [1,2,3;4,5,6] Define variable M to be a v(1,) The 1st to n-th value of vector v 1 2 3  GNU Octave reference manual matrix with values 4 5 6 The n-th value to the end of v(,end) https://www.gnu.org/software/octave/doc/ v = [:st]: Create a row vector vector v / with values from s M(,) The n, m-th value of matrix M to e with a step M(,:) The n-th row of matrix M MATLAB/GNU Octave wikibook size of st M(,:) The n-th row of matrix M https://en.wikibooks.org/wiki/MATLAB_ v = linspace(,[,st]) Create a row vector I = find(X > 2) Find indexes in X where the Programming/GNU_Octave with values from value is greater than 2 s to e with st Introduction to MATLAB intermediate values www.mathworks.com/moler/intro.pdf