Characters and arithmetic operators
Character | Description
/
‘Addition
Subtraction
Multiplicat
Division
Exponential
Creates vectors with equally spaced elements, represents rang of elements in arrays
Assignment operator
Parentheses; enclose input arguments in functions and subscripts of arrays
Brackets; forms arrays, enclose output arguments in functions
Separates array subscripts and function arguments; separates commands in the same
line
Suppresses display; ends row in array
Single quote; matrix transpose; creates string
Ellipsis; continuation of line
Percent; denotes a comment, specifies output format.
n (Scalar and array)
Element-by-element multiplication of arrays
Element-by-element division
Element-by-element exponentiation
Display formats in Command window
Command (format) | Description
‘compact Eliminates empty lines
loose ‘Adds empty lines
short Fixed point with 4 decimal digits
long Fixed point with 15 decimal digits
short e Scientific notation with 4 decimal digits
Jonge Scientific notation with 15 decimal digits
bank 2 decimal digits
long g Best of 15 digit fixed/floating
short g Best of 5 digit fixed/floating point
rot Conver} decimal te fachonsMathematics function and rounding
| Function Description
sqrt | Square root
exp fe
abs —_| Absolute value (eg. Ix!)
factorial | Factorial function
og | in
logio | log base 10
sin | sine (in radians)
sind e (in degrees)
asin | inverse sine (in radians)
asind | inverse sine (in degrees)
sinh | hyperbolic sine
round | Round to the nearest integer
fix | Round towards zero [eg. fix(-4.34)=-4]
ceil | Round towards positive infinity feg.ceil-4.3
floor | Round towards negative infinity eg. floor(-4.3
rem | Returns the remainder after x is divided by y
Managing compounds
Command | Description
de Clears the command window
clear | Remove all variables from memory
clearxyz | Removes variables x y z from the memory
close _| Closes the active Figure Window
cd Changes current directory
global —_| Declares global variables
who Displays variables currently in the memory
felose —_| Closes file
fopen | Opensa file
Predefined variables
Variable Description
| Pi 7
eps Smallest difference between 2 numbers
infor inf | Infinity
i
ans Value of last expression
NaN ornan | Not a number. Used to express mathematically undefined valuesCreating arrays
reshape(A, m, n)
rand (N)/(M,N)
randi (imax,N),
randn (N)/(M,N)
’ax,M,N)
Function Description
diag Creates a diagonal matrix from a vector.
Creates a vector from the diagonal of a matrix.
eye Creates a unit matrix (specify rows and columns if needed)
leg. eve (1,4) = 1000]
1 0 0 0
o 1 0 0
o 0 1 0
eye(4)= ° ° ° + (4 rows and 4 columns)
linspace(f, |, a) Creates equally spaced vector.
= first number, | = last number, n= number of terms
ones (F, ¢) Creates array with ones
zeros(r,¢) Creates array with zeros
length Number of elements in the vector
size Number of rows and columns in a matrix/vector
Rearrange matrix (change number of rows and columns)
Generate random numbers (0-1) in NxN or MxN matrix
Generate random integers (1-imax) in NxN or MxN matrix
Generate random numbers (mean 0, SD 1) in NxN or MxN matrix
Atm) All rows of column n
am, :) All columns of row m
A:, m:n) All rows of columns m ton
Almn, :) Al columns of rows m ton
Alm:n,, pq) Columns of p to q of rows m ton
Array functions
Function
dot (A,B) Dot product; computes inner product of 2 vectors. (a and b > MUST same size)
det (A) Calculate the determinant of matrix A
inv Inverse of square matrix
max Maximum value
min Minimum value
median Median value
sum(v) sum
sort(y) elements sorted into ascending order (by column first)
std standard deviation
cross(v,w) cross product, (v and w > must have 3 elements)Input to a script file
~~ variable_name=input(‘prompt’) a
= prompt > text that input command displays in command window
= puta (1) space or (2) a colon and a space at the end of prompt ~> separated from the prompt
Input to a script file (string)
+ method 1:
name = input (‘Your name: ‘)
- method 2:
variable_name = input(‘prompt, ‘s’
name = input("Your name: ‘,‘s') - ‘s': specify input is a string
{printf to save output to file
1, Open file:
fid=fopen('file_name', 'permission')
i 4
identer Tells how file will be used
r+ read (default if not specified)
wt write (overwrite content if any)
a: append (continue adding to content if any)
2. Write to file with fprint£. Use it exactly as before but
insert fd before the format string
tprintf (fid, ‘format string', variables)
Close file: £close (£id)
Input and output
‘Command Description
disp Displays output
‘printf isplays/saves output (print column by column)
Display Character
Single quotation mark |“
Percent i
[New line \n
Horizontal tab \t
Conversion specifier | Format —
oxyf Fixed point
x: min. number of characters to display
y: number of decimal points to display
ee ‘Scientific
ed Integers
6s String
input Prompts for user input
oy Retrieves variables to the workspace
save Saves variables in the workspace
xisread Imports data from Excel20 plot
Light ntonsty 2.2 Functon of Distance
SOs
TEXT
[LABEL
(Optional) ‘specifiers that
Veefor Vector define the type and color
(Optional) Properties with
values that can be used to
ofthe line and markers. specify the line width, and
marker’ size and edge, and
fill colors.
Marker type Specifier Line color Specifier
Plus sign + Red r -
Circle ° Green g
Asterisk : Blue b
Point i Cyan ke
Cross x Magenta ™m
Triangle (point up) * Yellow y
Triangle (point down) | v Black k
Triangle (pointleft) | <
Triangle (point right) __| > Line style Specifier
Square s Solid (default) -
Diamond d Dashed =
Five pointed star P Dotted
Sixcpointed star h Dash-dot =