Escolar Documentos
Profissional Documentos
Cultura Documentos
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