Você está na página 1de 6

2.

2 Arrays: vectors and matrices


As mentioned earlier, the name MATLAB stands for MATrix LABoratory because MATLAB has
been designed to work with matrices. A matrix is a rectangular object (e.g. a table) consisting of
rows and columns.
A vector is a special type of matrix, having only one row, or one column. Vectors are also called
lists or arrays in other programming languages.
MATLAB handles vectors and matrices in the same way, so we will use the term array generally,
with vector and matrix referring to their one-dimensional (1-D) and two-dimensional (2-D)
forms.
2.2.1 Initializing vectors: explicit lists and colon operator
Matrix is the basic element of Matlab i.e a scalar is a 1x1 dimensional matrix so, a column vector
of dimension n is an nx1 matrix while a row vector of dimension n is an 1xn matrix.
Unlike the other programming laguages as C++ , it is not compulsory in Matlab to declare the
variables befor they are used reason while you need to take all precautions while manipulating
these objets.
To state a vector, you separate the elements by a space or comas for a row vector i.e. line vector
and by semi colon (;) for column vectors. Eg.
>> V1 = [0 1 2]
V1 =
012
>> V2 = [0;1;2]
V2=
0
1
2
You can use one vector in a list for another one, e.g.:
1) a = [1 2 3];
b = [4 5];
c = [a -b];
2) a = [1 3 7];
b = [a 0 -1];
These are all examples of the explicit list method of initializing vectors.
Remember the following important rules:
Elements in the list must be enclosed in square not round brackets.
Elements in the list must be separated either by spaces or by commas.
A vector can also be generated (initialized) with the colon operator eg
>> V = [0:0.2:1]
V=
Columns 1 through 6
0 0.2000 0.4000 0.000 0.8000 1.0000
By default the increment is 1 so for a given
x = 1:10
The elements are 1,2,3,,10
Thus, if the colons separate three values, the middle value is the increment);
x = 10:-1:1
(elements are the integers 10, 9, , 1, since the increment is negative);
2.2.2. Linspace and Subscripts
The function linspace can be used to initialize a vector of equally spaced values, e.g.
V=Linspace (0, pi/2, 10)
With Matlab, we can refer to particular elements of a vector or modify it by means of subscripts.
1) Enter r = rand(1,7)
This gives you a row vector of seven random numbers.
2) Now enter r(3)
This will display the third element of r. The number 3 is the subscript.
>> V = [0:5]
V=
0 1 2 3 4 5
>> a = V(2);
>> V(3) = 3*a
V=
0 1 3 3 4 5

2.2.3. Transpose
The transpose of a vector is generate with the single quote, or apostrophe (), or with the function
transpose
>> x = 1:5
>>x
Or >> transpose(x) to display the transpose of x.
2.2.4. Matrices
Since matlab is an analitical tool, all the numerical operations needed on vectors are well defined
in it. Ie: Addition , subtraction, multiplication and division. E.g.
>> V1 = [1 2];
>> V2 = [3 4];
>> V3 = V1 + V2 % addition of vectors
V3 =
4 6
>> V4 = V2 - V1 % subtraction of vectors
V4 =
2 2
>> V5 = 2*V1 % multiplication by scalar
V5 =
2 4

Note that, with respect to multiplication and division, you must be careful with the dimensions of
the vectors. For multiplication or division element by element, you should add a dot (.) in front
of the oparator ie. (.* and ./).
Example:
>> S1 = V1.*V2 % multiplication element by element
S1 =
3 8
>> S2 = V1./V2 % division element par element
S2 =
0.3333 0.5000
If the dimensions of the concerned vectors are not correct, Matlab informs you of errors by a
message. E.g.
>> T = [1 2 3]
T=
1 2 3
>> T1 = V1.*T
??? Error using ==> .* Matrix dimensions must agree.
So pay attention to the message because it helps you in corrections.
Since it is possible to join vectors, you can generate matrices by joingning up vectors. Eg
1) >> V1 = [1 2];
>> V2 = [3 4];
>> V = [V1 V2]
V=
1 2 3 4
2) >> V1 = [1;2];
>> V2 = [3;4];
>> V = [V1;V2]
V=
1
2
3
4

3) >> V1 = [1 2];
>> V2 = [3 4];
>> V = [V1;V2]
V=
1 2
3 4
Since a matrix is a table consisting of rows and columns, you can also create a matrix directly
just as you do a vector, except that a semicolon is used to indicate the end of a row, e.g.
>> M = [1 2 6; 3 4 9]
M=
1 2 6
3 4 9
You can evidently have access to the elements of matrix.
Eg.
>> m21 = M(2,1) % 2n row, 1st column
m21 =
3
A matrix may be transposed, e.g. with a initialized as above, the statement a=M result in :
a=
1 3
2 4
6 9
As well as with vectors we can determine the inverse and the transpose of a matrix with the
functions inv () and transpose() or (). Eg:
>> M = [1 2; 3 4]
M=
1 2
3 4
B= inv(M)
B=
-2.0000 1.0000
1.5000 -0.5000
Thus in Matlab, operation on matrices are predifined. Subtraction and addition are direct
operations as well as multiplication by a scalar.
while multiplication and division are defined according to matrix multiplication and division.
E.g. Given A and B respectively:
>> A = [1 2; 3 4];
>> B = [4 3; 2 1];
>> C = A+B % addition
C=
5 5
5 5
>> D = A-B % subtraction
D=
-3 -1
1 3
>> C1 = 3*A % multiplication by a scalar
C1 =
3 6
9 12
>> C = A .*B % multiplication element by element
C=
4 6
6 4
>> D = A ./B % division element by element
D=
0.2500 0.6667
1.5000 4.0000

>> C2 = A*B % multiplication of matrices


C2 =
8 5
20 13
>> D1 = A/B % division of matrices
D1 =
1.5000 -2.5000
2.5000 -3.5000

Você também pode gostar