MATLAB Basic Commands
MATLAB Basic Commands
e.g Example
Command Usage Notes
# Command Result
+ Addition 3+4 7 In case of matrix addition or
1 [1 2 3]+[4 5 6] [5 7 9] subtraction, both matrices have to
- Subtraction 7-3 4 have same dimensions
Unary Multiplication 2*4 8
( ) ( ) ( )
2 * [1 2 3] *[4 For more info about the brackets [],
Matrix Multiplication 5 32 See example # 7
6] Refer to Matrices multiplication for
more information
3 / Division 8/2 4 You can use the brackets ( ) as
4 ^ Power (exponent) 2^3 8 (2+3)^2/5
Wherever you enter the variables
( a or b or My_var ) in your
code, they will represent their
a=10 a=10 values.
Assign a value to a
5 = b=3+2 b=5 You can name the variable by any
variable My_var=a/b My_var=2 name, but be aware that variables
are case sensitive and have some
restrictions, so any mismatch will
cause an error.
y=sin(0) y=0
sin( )
6 trigonometric functions y2=sin(pi/2) y2=1
cos( )
y3=cos(0) y3=1
The input argument has to be in
radian
pi is a constant equal to
- Here we defined a matrix named
MyMatrix has 1 row and 3
7 [ ] Define a new matrix MyMatrix= [ 2 5 3] MyMatrix= [ 2 5 3] coulomns .
- You can use space to separate
between matrix’s element
Same as the previouse example but
8 , Define a new columns MyMatrix= [2,5,3] MyMatrix= [ 2 5 3]
here we used comma instead of space
you can use enter instead as
MyMatrix=[1,2,3;4, MyMatrix=[1 2 3
Define a new row 5,6;7,8,9] MyMatrix= [ ]
4 5 6
7 8 9]
9 ; Used at the end of
Nothing will appear
command line to It’s very useful to boost the speed of
in command window,
avoid presenting the A=[1 2]; your code’s execution as well as to
but the matrix A
outcome in command will be defined. avoid redundant outcomes
window
Create a vector of serial
10 : 1:5 [ ]
numbers
1:2:9 [ ]
Create a vector with
11 : :
certain increment step a=-0.6; b=0; c=0.2;
[ ]
a:c:b
It can be used after matrix’s name
Matrix transpose. i.e. [ 1 2 3]’ [ ] e.g. MyMatrix’
12 ‘ change the rows to be i.e. in all examples you can use the
columns and vice versa [1 2 ; 3 4]’ [ ] matrix’s name instead of matrix
itself
In arry multiplication each element
in the matrix is multiplied by the
13 .* Array Multiplication [1 2 3].*[ 3 4 5] [ ] corresponding one in the other
matrix, thus the diamensions both
matricies must agree.
14 .^ Array Power [1 2 3].^2 [ ] You can use also 2.^[2 3 4]
e.g Example
Command Usage Notes
# Command Result
A= [3 4 5 Return the element in row
Used after the matrix’s
7 3 4 number 3 and column number 2
15 ( , ) name to return a specific 1
2 1 5];
element in that matrix A(3,2) in the matrix A
x=[2 5 10 4]; Many useful ways to use max()
max( ) Find the maximum and
16 max(x) 10
min( ) minimum values and min(). See MATLAB help
min(x) 2
sum([ 1 5 3]) 9 It can be used an integral
17 sum( ) Gives the addition result In case of matrix has more than
A=[1 2 3; 4 5 6 ]; one row, it adds each colum’s
sum (A) [ 5 7 9] elements separately.
The condition A>4 i.e. all
A=[1 4 6]; elements in the matrix A greater
Fine elements that than 4.
satisfy a certain C1=find(A>4) C1=3
18 find( ) & means both condition have to
condition and return its A(C1) 6 be satisfied.
index C2=find(A>1 & A<6) C2=2
Use MATLAB help to find more
A(C2) 4 info.
Note that each (%) will eleminate
% add any comment No execution will be carried anything on its right for one line
19 % Comment % or explanation out, any command or line only. i.e. if your comments are more
% for your code begin with % will be ignored than one line you have to add % for
each line as in this example.