Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Ton Duc Thang University

Faculty of Information Technology

Supplementary Material

1 Creating vectors
• Method 1: Creating vectors manually
You can create the row vector x=[2, 3, 5], type MATLAB’s command at Command Prompt
>> x=[2 3 5 ]

x =

2 3 5

or
>> x =[2 , 3 , 5 ]

x =

2 3 5
 
4
 9 
 7 , type MATLAB’s command.
and create the column vector  

12
>> x=[4
9
7
12]

x =

4
9
7
12

or type another MATLAB’s command


>> x = [ 4 ; 9 ; 7 ; 1 2 ]

x =

4
9

1
Ton Duc Thang University
Faculty of Information Technology

7
12

• Method 2: Creating vectors using linear method


To create a vector using this method, you need to determine the start value, the increment, and
the final value for the vector.
For example,you should create the vector x = [0, 2, 4, 6, 8, 10], type MATLAB’s command
>> x = 0 : 2 : 1 0

x =

0 2 4 6 8 10

• Method 3: Creating vectors using the linear spacing method.


The MATLAB function linspace(x1,x2,N) can be used to create a row vector. the function has three
arguments which are

– x1 is the start value.


– x2 is the final value.
– N is the number of elements in a vector
For example, You should create the vector x = [0, 2, 4, 6, 8, 10], type MATLAB’s command
>> x=linspace ( 0 , 1 0 , 6 )

x =

0 2 4 6 8 10

• Method 4: Creating vectors using the Logarithmic spacing method


MATLAB provides the logspace(a1,a2,N) function creates a row vector of logarithmically equally spaced
points between 10a1 and 10a2 . Three arguments are used to determine vector creation as follows:
– The start value for the created vector is 10a1
– The final value for the created vector is 10a2
– The number of elements in the created vector is N.
For example, you should create the vector x = [10, 100, 1000, 10000, 100000], type at the MATLAB
Command Prompt
>> x=logspace ( log10 ( 1 0 ) , log10 ( 1 0 0 0 0 0 ) , 5 )

x =

10 100 1000 10000 100000

On the other hand, MATLAB provides some functions to make the default vectors as zeros, ones, rand , nan.

2
Ton Duc Thang University
Faculty of Information Technology

2 Accessing Elements in Vectors


Once you have created a vector, you can access an individual element within that vector. In MATLAB, the
index of vector start with 1, not 0.

When you access to the nth elements, you can type a(n). The index argument can be a vector. In this case,
each element is looked up individually, and returned as a vector of the same size as the index vector.
>> x=[1 5 4 7 6 8 9 10 32 5 7 ] ;
>> x ( 1 : end )

ans =

1 5 4 7 6 8 9 10 32 57

>> x ( 5 )

ans =

>> x ( 1 : 2 : end )

ans =

1 4 6 9 32

>> x ( 2 : 7 )

ans =

5 4 7 6 8 9

3 Relational and Logical Operations on Vectors


Remember that MATLAB has six relational operators which are

1. Greater than (>)


2. Less than (<)
3. Greater than or equal to (>=)
4. Less than or equal to (<=)

5. Equal to (=)

3
Ton Duc Thang University
Faculty of Information Technology

6. Not equal to (∼=)

and three local operators which are


1. AND (&)
2. OR ( | )
3. NOT ( ∼ )
 
For example, let two vector x= 0 4 7 0 −1 2 and y= 1 2 8 0 −4 6 . Use MATLAB’s
command to find the results of the following operators
• x == y
• x<y

• x>y
• x <= y
• x >= y

• x ∼= y
• x&y
• x|y
In MATLAB, you can easily search for an individual element, or a group of elements, in a vector, depending
on their values. 
For example, Let a vector x= 2 3 5 5 7 10 12 find the indices of the elements whose value are
equal to 5
>> x=[2 3 5 5 7 10 1 2 ] ;
>> find ( x==5)

ans =

3 4
For another example, to find the indices of the elements in the x below whose values greater than 7. In
Command Prompt, you can type command
>> find ( x>7)

ans =

6 7

You might also like