4 basic mathematics operations
5 + 7
ans = 12
8 - 3
ans = 5
6 * 3
ans = 18
1/3
ans = 0.33333
2^4
ans = 16
3 basic logistics operations
1 ~= 2
ans = 1
1 == 2
ans = 0
1 &&0
ans = 0
1 || 4
ans = 1
xor(1, 0)
ans = 1
The basic uses for variables
a = 3
a = 3
a = 5; % semicolon suppressing output
a = ‘hi’
a = hi
a = pi
a = 3.1416
disp(a)
3.1416
disp(sprintf(‘2 decimals: %0.2f’, a))
2 decimals: 3.14
format long
a
a = 3.141592653589793
a = [1 2; 3 4; 5 6]
a =
1 2
3 4
5 6
v = [1 2 4]
v =
1 2 4
(v’)’
ans =
1 2 4
v = 1:0.1:2
v =
Columns 1 through 4:
1.000000000000000 1.100000000000000 1.200000000000000 1.300000000000000
Columns 5 through 8:
1.400000000000000 1.500000000000000 1.600000000000000 1.700000000000000
Columns 9 through 11:
1.800000000000000 1.900000000000000 2.000000000000000
v =- 1:5
v =
-1 0 1 2 3 4 5
ones(2, 3)
ans =
1 1 1
1 1 1
c = 2 * ones(2, 3)
c =
2 2 2
2 2 2
eye(3)
ans =
Diagonal Matrix
1 0 0
0 1 0
0 0 1\
rand(3, 3)
ans =
0.411327 0.048251 0.520388
0.436561 0.625215 0.394285
0.543362 0.511269 0.920021
randn(1, 3)
ans =
1.53855 -0.72854 -0.19124