Vectors
In Matlab,
you can form arrays of numbers - vectors or matrices - in a
straight and intuitive way:
>> a =
[2 4
6 9 -1 3]
means a row vector with values in variable 'a'
a = 2
4
6 9
-1 3
The numbers of the array
should be separated by spaces
(like shown) or
commas.
If you're working with complex
numbers, you must avoid spaces
and work only with commas.
There are simple ways to create horizontal arrays (row vectors) that
increase or decrease in a linear pattern. For example, let's say that
you wish to have a vector t
to contain eleven values equally spaced
from 0 to ¶. You can do this in at least three ways:
>> t = [0
.1*pi .2*pi .3*pi .4*pi
.5*pi .6*pi .7*pi .8*pi
.9*pi pi]
>> t = (0 : 0.1 : 1)*pi
>> t = linspace(0,pi,11)
The Matlab result is exactly the same:
t =
Columns 1 through 7
0 0.3142
0.6283
0.9425
1.2566
1.5708 1.8850
Columns 8 through 11
2.1991
2.5133
2.8274 3.1416
In the first case, you only place the numbers one by one, which is a
very boring way to do it...
In the second case, you create an array which starts with 0, an then
you increment the value in 0.1 steps until you get to 1. Then, every
number is multiplied by ¶.
In the third case, you use the linspace
function, which has the
following arguments:
linspace(start_value, end_value, number_of_values)
Obviously, the latest two ways are much better than the first way to do
it.
Vector
Construction
You create a row vector
x
specifying the numbers, one by one:
x = [1 3 9
33 0 -2]
You create a row vector x
starting with a first value until you get to
a last value, in steps of 1.
x = first: last
You create a row vector x
starting with a first value until you get to
a last value, in specified increments.
x = first: increment: last
You create a row vector x
starting with a first value until you get to
a last value, in equally spaced n
elements.
x =
linspace(first, last, n)
To access each element of the vector you use subscripts. For
example,
x(1) is the first element of the vector x.
To access a group of contiguous
elements, you also use subscripts. For
example, x(1:5) represents five elements, from element 1 to element 5
of the array.
Other examples:
x(4 : -1 : 1) represents elements 4, 3, 2 and 1, in this order.
x(2 : 2 : 7) represents elements 2, 4, and 6.
To work with column
vectors, you put a ';'
between each element.
>> b = [1; 2;
3; 4; 5]
b =
1
2
3
4
5
>>
You may use the transpose operator ('),
to convert a row vector into a
column vector.
>> a = 1:5
a =
1 2
3 4 5
>> b = a'
b =
1
2
3
4
5
>> x=(2:3:15)'
x =
2
5
8
11
14
>>
From
'Vectors' to home
From
'Vectors' to 'Matlab Help Menu'
|
|