Matlab Basics

Common commands

In the Matlab, some commands you may use very often.

Command Meaning
clc Clear all contents from Command Window, resulting a clear screen
clear Clear all contents from workspace, freeing up system memory
who/whos Alphabetically list the names, sizes, and types of all variables in the currently active workspace
dir List files and folders in the current folder
cd Change the current working directory
help/doc Display the help text for the functionality
quit/exit Terminate MATLAB program

Here are some other useful commands.

  • ,’ or ‘no sign’ is used to show the calcualtion results.
  • ;’ is for hiding the calculation results.
  • %’ is for adding the note for your code.
  • ...’ is for continuing the code.

Variable

You can create new variables in the workspace by running MATLAB code or using existing variables. To create a new variable, enter the variable name in the Command Window, followed by an equal sign ( = ) and the value you want to assign to the variable. The length of value in the Matlab is decimal. The number can be reoresented by the sign of ‘.’ and ‘-’ and the format of default value is double. For example, 7, -16, 0.007, 1.7e12 ( 1.7 * 10 12 ), 1.732e-8 ( 1.732 * 10 -8 ).

The rule of naming variables

  • The maximum length of the variable name is 63 (The version of 6.5 and 6.5+).
  • The first symbol of the name must be letter.
  • The variable name must not include blank space and punctuation.
  • The name of variable or function is case sensitive. For example, myBristol is different from mybristol.

Special variables

Fuction Meaning
ans Most recent answer
pi Ratio of circle's circumference to its diameter
eps Floating-point relative accuracy
inf The scalar representation of positive infinity. Operations return Inf when their result is too large.
NaN The scalar representation of "not a number". Operations return NaN when they have undefined numeric results, such as 0/0 or 0*Inf.
i, j i=j=
nargin Number of function input arguments
nargout Number of function input arguments
realmin Smallest normalized floating-point number
realmax Largest positive floating-point number

Operators

Mathmatical operatiors

Operators Meaning
a + b a plus b
a - b a minus b
a * b a times b
a / b or a \ b a is divided by b
a^b b th power of a

Relational operators

Operators Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to

Logical operators

Operators Meaning
& Logical AND
| Logical OR
~ Logical NOT

Common Mathmatical Functions

Fuction Meaning
sin(x) sine
asin(x) sine' inverse trigonometric functions
cos(x) cosine
acos(x) cosine' inverse trigonometric functions
tan(x) tangent
atan(x) tangent' inverse trigonometric functions
abs(x) Absolute value
max(x) Maximum elements of an array
min(x) Minimum elements of an array
sum(x) Sum of array elements
sqrt(x) Sum of array elements
sum(x) Square root of each element of the array X
exp(x) Exponential e x for each element in array X
log(x) Natural logarithm ln(x) of each element in array X
log 10 (x) Base 10 logarithm function
Y = sign(x) Returns an array Y the same size as x
Y = fix(x) Rounds each element of X to the nearest integer toward zero

Array and Matrix

Array

There are some commands can be used to generate array or matrix. Once the array or matrix is generated, it would be stored in workspace as the variable. We can check these variables with who/whos command or directly with workspace window. The table below includes some basic commands that can be used to generate simple array or matrix.

The generation of an array

Command Meaning
x = [a,b,c,d] Create a vector with specific factors
x = a:b Generate a vector from a to b with increment 1
x = a:m:b Generate a vector from a to b with increment m

x=linspace(a,b,n) generates a vector from a to b with n factors. The interval is (b-a)/(n-1). For example,

x = linspace(1, 10, 5) 
      

A vector from 1 to 10 is generated with 5 factors. The interval is 9/4.

x = 1.0000    3.2500    5.5000    7.7500   10.0000
      

The operation of an array

Some commands can be used to deal with an array.

  • x(i) is used to exact i th factor in the array x.
  • x(i: m :j) is used to exact the factors from i to j and the space is m.
  • x([a b c d]) is used to direclty exact a th, b th, c th and d th factor in the vector to create a mew matrix, which is same to command [x(a) x(b) x(c) x(d)].

An array can be calculated with a single numeric value. If a = [a1, a2,..., an] is an array and c is a single numeric value.

a + c = [a1+c, a2+c,..., an+c]
a.* c = [a1*c, a2*c,..., an*c]
a./ c = [a1/c, a2/c,..., an/c]
a.\ c = [c/a1, c/a2,..., c/an]
a.^ c = [a1^c, a2^c,..., an^c]
c.^ a = [c^a1, c^a2,..., c^an]

An array can also be calculated with another array. If a = [a1, a2,..., an] is an array and b = [b1, b2,..., bn] is another array.

a + b = [a1+b1, a2+b2,..., an+bn]
a.* b = [a1*b1, a2*b2,..., an*bn]
a./ b = [a1/b1, a2/b2,..., an/bn]
a.\ b = [b1/a1, b2/a2,..., bn/an]
a.^ b = [a1^b1, a2^b2,..., an^bn]
b.^ a = [b1^a1, b2^a2,..., bn^an]

Matrix

The generation of a matrix

To create a normal matrix. , or space is used to divide factors in each row. ; is used to divide each row. The number of factors in each row must be same.

a = [1,2,3; 4,5,6; 7,8,9]
      

Another way to create a matrix is drectly using matrix format.

b = [1 2 3 
           4 5 6 
           7 8 9]
      

Some commands can be used to genearte common matix.

Command Meaning
a = [] Create a empty matrix
ones(m, n) Create a m-by-n matrix with all ones
zeros(m, n) Create a m-by-n matrix of all zeros
eye(m, n) Return an m-by-n matrix with ones on the main diagonal and zeros elsewhere

The operation of a matrix

Some commands can be used to deal with a matrix. If A is a matix,

  • A(i, :) is the i th row of matrix A.
  • A(:, j) is the j th colum of matrix A.

Some commands can be used to exact factors in the matrix.

Command Meaning
A(r , :) Extract r th row of matrix A
A(: , c) Extract c th column of matrix A
A(r1 : rm, c1 : cn) Create a new matrix by extracting the row from r1 th row to rm th row and c1 th column to cn th column
A(r1 : rm, :) = [] Create a new matrix by extracting rows from r1 to rm
A(:, c1:cn) = [] Create a new matrix by extracting rows from r1 to rm
[A B]/[A: B] Create a new matrix by combining matrix A with B
A(:) Create a new column vector by extracting each row of matrix A
A(rm: -1 : r1, :) Create a new matrix by inversely extracting rm th row to r1 th row
A(: , cn: -1: c1) Create a new matrix by inversely extracting cm th row to c1 th row


Previous | Up | Next