Professional Documents
Culture Documents
Computer Application
Array
An array is a collection of data values organized into rows and columns and
known by a single name. Three fundamental concepts in MATLAB, and in linear
algebra, are scalars, vectors and matrices.
A scalaris simply just a fancy word for a number (a single value).a scalar is a
matrix with a row and column dimension ofone (1-by-1 matrix).
A vectoris an ordered list of numbers (one-dimensional). In MATLAB they can be
represented as a row-vector or acolumn-vector.
A matrixis a rectangular array of numbers (multi-dimensional). In MATLAB, a two-
dimensional matrix is defined byits number of rows and columns.
Array
NON-ACTIVATED VERSION
www.avs4you.com rotceV Matrix
The size of an array is the number of rows and the number of columns in the
array.
5
7
4
Lecture 2
Computer Application
1 2 3 4 5
1 2 3 4
5 6 7 8
9 10 11 12
x=
NON-ACTIVATED VERSION
www.avs4you.com
2 4 8
x=
2 4 8
To create a column vector, you simply type the elements inside a pair square
bracket separating them with a semicolon (;).
>> x=[2;4;8]
x=
To create two dimensional arrays (matrix), type the matrix row by row separating
Lecture 2
Computer Application
the elements in a given row by spaces or commas and separating the rows by a
semicolon.
>> x=[1 2 3;4 5 6;7 8 9]
x=
1 2 3
4 5 6
7 8 9
NON-ACTIVATED VERSION
If the step size is one, then it is not required to write the step size. Instead type
only the 1st value and the last value.
x = 1st www.avs4you.com
value: last value
Hint: the step size should be positive if the last value is greater than the 1st value.
The step size should be negative if the 1st value is greater than the last value.
Example: The following statements explain how you can create vectors.
>> x=[1:4]
x=
1 2 3 4
>> y=[1:2:7]
y=
1 3 5 7
>> z=[5:-1:1]
z=
Lecture 2
Computer Application
5 4 3 2 1
>> a=[10:5:40]
a=
10 15 20 25 30 35 40
>> b=[2:-0.5:-0.5]
b=
command
NON-ACTIVATED VERSION .
object
zeros (n, m)www.avs4you.com
create a matrix with n rows and m columns, which all the
elements are the numbers 0
ones (n, m) create a matrix with n rows and m columns which all the
elements are the numbers 1
eye (n) Creates a square matrix with n rows and n columns in which the
diagonal elements are equal to 1, and the rest of the elements
are 0. This matrix is called the identity matrix
Example:
Using the ones and zeros commands, create a 4 * 5 matrix in which the first two
rows are 0's and the next two rows are 1 's.
>> a=ones(2,5)
a=
1 1 1 1 1
1 1 1 1 1
>> b=zeros(2,5)
b=
Lecture 2
Computer Application
0 0 0 0 0
0 0 0 0 0
>> c=[b;a]
c=
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
1 1 1 1 1
H.W.Create a 6 * 6 matrix in which the middle two rows, and the middle two
columns are l's, and the rest are 0's. (Use three statements only)
NON-ACTIVATED VERSION
Once a vector or a matrix is created you might needed to extract only a subset of
the data, and this is done through indexing.
www.avs4you.com
Example:
>> x=[0:pi/6:pi]
x=
>> y=sin(x)
y=
ans =
1.0472
Lecture 2
Computer Application
ans =
0.8660
>>x(1:4) show the 1st, 2nd, 3rd , and 4th elements of vector x
ans =
ans =
NON-ACTIVATED VERSION
1.0472 1.5708 2.0944 2.6180 3.1416
www.avs4you.com
>>x(1:2:5)show the 1st, 3rd , and 5th elements of vector x
ans =
0 1.0472 2.0944
ans =
ans =
>> a=[1:5]
a=
1 2 3 4 5
>> b=[0:10:50]
b=
0 10 20 30 40 50
c=
1 2
NON-ACTIVATED VERSION
3 4 5 0 10 20 30 40 50
www.avs4you.com
>> m=[a(1:2:5) 1 0 1]
m=
1 3 5 1 0 1
Example
>> x=[1 2 3;4 5 6;7 8 9]
x=
1 2 3
4 5 6
7 8 9
x=
1 2 3
4 5 6
7 8 0
7 0
4 6
1 3
8 0 www.avs4you.com
>> z=[1 3]
z=
1 3
>> y=x(z,z)
y=
1 3
7 0
y=
Lecture 2
Computer Application
1 3
4 6
7 0
ans =
3 3
H.w
NON-ACTIVATED VERSION
www.avs4you.com