In MATLAB there are 3 basic types of numbers that you will be working with, which are all actually matrices. There are scalars, which are just plain numbers, vectors, which are kind of like a list of numbers, and matrices, which are tables of numbers. Let's create a simple scalar:
x = 6
size(x)
ans =
1 1
v = [1 2 3];
size(v)
ans =
1 3
v = [1 2 3]
v =
1 2 3
A = [1 2; 3 4]
ans =
1 2
3 4
size(A)
ans =
2 2
P = [1 2 3 4]; Q = [5 6 7 8]; R = [9 11 11 12];
S = [P; Q; R]
S =
1 2 3 4
5 6 7 8
9 10 11 12
size(S)
ans =
3 4
From math courses, you should know that matrices' dimensions are very important when we are multiplying them or doing other operations. And typically, you work with a column vector, not a row vector like the one that we created before. So we can easily transform it into a column vector using apostrophe to perform a transpose.
v = v'
v =
1
2
3
size(v)
ans =
3 1
v = [1; 2; 3]
v =
1
2
3
A(1,1)
ans = 1
A(1,:)
ans =
1 2
A(:,2)
ans =
2
4
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 4 15 16]
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 4 15 16
A(:,:)
ans =
1 2 3 4
5 6 7 8
9 10 11 12
13 4 15 16
A(2:3, 2:3)
ans =
6 7
10 11
A(3,:)
, we'll get all elements from the third row.
A(3,:)
ans =
9 10 11 12
1:10
, it gives back all the numbers from 1 to 10.
1:10
ans =
1 2 3 4 5 6 7 8 9 10
w
.
w = 1:10
size(w)
w =
1 2 3 4 5 6 7 8 9 10
ans =
1 10
linspace
. It is similar to the colon operator (:), but gives direct control over the number of points. For example,
linspace(0, 2*pi)
ans =
Columns 1 through 8:
0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443
...
...
Columns 97 through 100:
6.0928 6.1563 6.2197 6.2832
0
and 2*pi
.
linspace(0, 2*pi, 8)
ans =
0 0.8976 1.7952 2.6928 3.5904 4.4880 5.3856 6.2832
0
and 2*pi
. This is useful when we want to divide an interval into a number of subintervals of the same length.
Need help? Use help
command!
help colon
'colon' is a built-in function from the file libinterp/corefcn/data.cc
-- R = colon (BASE, LIMIT)
-- R = colon (BASE, INCREMENT, LIMIT)
Return the result of the colon expression corresponding to BASE,
LIMIT, and optionally, INCREMENT.
This function is equivalent to the operator syntax 'BASE : LIMIT'
or 'BASE : INCREMENT : LIMIT'.
See also: linspace.