Matlab-Cheatsheet

Submitted by: Submitted by

Views: 155

Words: 941

Pages: 4

Category: Science and Technology

Date Submitted: 10/17/2013 11:07 AM

Report This Essay

A Matlab Cheat-sheet (MIT 18.06, Fall 2007)

Basics:

save 'file.mat' save variables to file.mat load 'file.mat' load variables from file.mat diary on record input/output to file diary diary off stop recording whos list all variables currenly defined clear delete/undefine all variables help command quick help on a given command doc command extensive help on a given command

Constructing a few simple matrices:

rand(12,4) a 12×4 matrix with uniform random numbers in [0,1) randn(12,4) a 12×4 matrix with Gaussian random (center 0, variance 1) zeros(12,4) a 12×4 matrix of zeros ones(12,4) a 12×4 matrix of ones eye(5) a 5×5 identity matrix I (“eye”) eye(12,4) a 12×4 matrix whose first 4 rows are the 4×4 identity linspace(1.2,4.7,100) 7:15 row vector of 7,8,9,…,14,15 diag(x) matrix whose diagonal is the entries of x (and other elements = 0)

Defining/changing variables:

x x x x A = = = = = 3 define variable x to be 3 [1 2 3] set x to the 1×3 row-vector (1,2,3) [1 2 3]; same, but don't echo x to output [1;2;3] set x to the 3×1 column-vector (1,2,3) [1 2 3 4;5 6 7 8;9 10 11 12];

row vector of 100 equally-spaced numbers from 1.2 to 4.7

Portions of matrices and vectors:

x(2:12) x(2:end) x(1:3:end) x(:) A(5,:) A(5,1:3) A(:,2) diag(A)

set A to the 3×4 matrix with rows 1,2,3,4 etc. x(2) = 7 change x from (1,2,3) to (1,7,3) A(2,1) = 0 change A2,1 from 5 to 0

Arithmetic and functions of numbers:

3*4, 7+4, 2-6 8/3 multiply, add, subtract, and divide numbers 3^7, 3^(8+2i) compute 3 to the 7th power, or 3 to the 8+2i power sqrt(-5) compute the square root of –5 exp(12) compute e12 log(3), log10(100) compute the natural log (ln) and base-10 log (log10) abs(-5) compute the absolute value |–5| sin(5*pi/3) compute the sine of 5π/3 besselj(2,6) compute the Bessel function J2(6)

the 2nd to the 12th elements of x the 2nd to the last elements of x every third element of x, from 1st to the last all the elements of x the row vector of every element in the 5th row of A...