Matlab Quick Start

For an overview of Matlab, run the command:
>> intro

For demos of specific features, run the command:
>> demo

To assign a numerical quantity to a variable
>> x=1

To suppress output, end with a semicolon
>> x=1;

To list all variables:
>> who
or for more detailed information
>> whos

To insert a comment:
>> % this is a comment

To display the value of a variable or expression, e.g. "x",
>> disp(x)
Note, the variable name will not be printed.
To create a Matlab script (an m-file), use a text editor to create a list of commands into a file, and give it a .m suffix
>> % This is an example
>> x = 1;
>> y = 2;
>> disp(x+y)

To navigate in the Matlab command window:
>> cd % change directory to home directory
>> pwd % print current directory
>> cd mydir % change directory to mydir subdirectory
>> dir *.m % list m-files in current directory

Create a row vector
>> x = [ 1,2,3 ] >> x = [ 1 2 3 ]
Note: both of these forms are equivalent!
To create a column vector
>> x = [1;2;3] >> x = [1 2 3]'
Note: both of these forms are equivalent!
To create a two-dimensional matrix
>> x = [1,2;3,4]

To access a vector component
>> x(2)

To access the 3rd through 5th elements
>> x = [1 2 3 4 5]
>> x(3:5)

To find the inverse of a matrix
>> A = [1,2;3,4]
>> inv(A)

To solve a matrix equation, A*x=b
>> A = [1,2;3,4]
>> b = [5;6]
>> x = inv(A)*b
>> x = A\b
Note: both of these forms are equivalent!
Note: the use of the backslash notation (unique to Matlab)
To create a uniform random number between 0.0 and 1.0
>> x = rand