You are on page 1of 9

Lecture 2

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

lanoisnemid enO Multidimensional


yarra matrix

Column vector Row vector

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

3*1 column 1*5 row vector


3*4 Matrix
vector
Creating Vectors & Matrixes in MATLAB
To create a row vector, you simply type the elements inside a pair square bracket
[ ] separating them with a space or comma (,).
>> x=[2 4 8] row vector separate by a space.

x=
NON-ACTIVATED VERSION
www.avs4you.com
2 4 8

>> x=[2,4,8] row vector separate by comma.

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

The colon (: ) is usually used to generate an array having regularly spaced


elements.
x = [1st value: step size: last value] Orx = 1st value: step size: last value

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=

2.0000 1.5000 1.0000 0.5000 0 -0.5000

The zeros, ones and eye Commands


The zeros (n, m), the ones (n, m), and eye (n) commands can be used to create
matrices that have elements with special values.

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)

Indexing Into a Vector or Matrix

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=

0 0.5236 1.0472 1.5708 2.0944 2.6180 3.1416

>> y=sin(x)

y=

0 0.5000 0.8660 1.0000 0.8660 0.5000 0.0000

>>x(3) show the 3rd element of vector x

ans =

1.0472
Lecture 2
Computer Application

>>y(5) show the 5th element of vector y

ans =

0.8660

>>x(1:4) show the 1st, 2nd, 3rd , and 4th elements of vector x

ans =

0 0.5236 1.0472 1.5708

>>x(3:end)show the elements of vector x from the 3rd up to last element

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

>>y(6:-2:1)show the 6th ,4th , and 2nd elements of vector y

ans =

0.5000 1.0000 0.5000

>>y([2 3 4])show the 2nd ,3rd , and 4thelements of vector y

ans =

0.5000 0.8660 1.0000


Lecture 2
Computer Application

>> a=[1:5]

a=

1 2 3 4 5

>> b=[0:10:50]

b=

0 10 20 30 40 50

>> c=[a b] create vector c from vectors a and b

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(3,3)=0change number stored in matrix x (at row=3 , column=3) to a new value=0


Lecture 2
Computer Application

x=

1 2 3

4 5 6

7 8 0

>> y=x(3:-1:1,[1 3])extract all elements in rows to 1 and in columns 1and 3


y=

7 0

4 6

1 3

>> y=x(2:3,2:3)extract all elements in rows 2 to 3 and in columns 2 to 3


y=
NON-ACTIVATED VERSION
5 6

8 0 www.avs4you.com
>> z=[1 3]

z=

1 3

>> y=x(z,z)

y=

1 3

7 0

>> y=x(:,z) ( : ) means all rows

y=
Lecture 2
Computer Application

1 3

4 6

7 0

>>size (x) return the size of the matrix x (rows, columns)

ans =

3 3

H.w

NON-ACTIVATED VERSION
www.avs4you.com

You might also like