Professional Documents
Culture Documents
Supplementary Material: 1 Creating Vectors
Supplementary Material: 1 Creating Vectors
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
x =
4
9
1
Ton Duc Thang University
Faculty of Information Technology
7
12
x =
0 2 4 6 8 10
x =
0 2 4 6 8 10
x =
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
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
5. Equal to (=)
3
Ton Duc Thang University
Faculty of Information Technology
• 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