Matrices: Matrices in MATLAB
Computing with matrices and vectors
You can easily add matrices in MATLAB, subtract then, multiply them (also scalar multiplication) and exponentiate them. Examples illustrate this best.
Computing with matrices and vectors
>> A = [1 2 3; 4 5 6]
A =
1 2 3
4 5 6
>> B = diag([1 2 3])
B =
1 0 0
0 2 0
0 0 3
>> At = A' % transposed of A
At =
1 4
2 5
3 6
>> A*B % matrix multiplication
ans =
1 4 9
4 10 18
>> A*At
ans =
14 32
32 77
>> At*A
ans =
17 22 27
22 29 36
27 36 45
>> ans + B % addition of matrices
ans =
18 22 27
22 31 36
27 36 48
>> 3*A % scalar multiplication
ans =
3 6 9
12 15 18
>> v = [1; 2; 3]
v =
1
2
3
>> A*v % matrix-vector multiplication
ans =
14
32
>> v'*A' % vector-matrix multiplication
ans =
14 32
>> B^3 % exponentiation: B^3 = B*B*B
ans =
1 0 0
0 8 0
0 0 27
>> B^(-1) % inverse as power
ans =
1.0000 0 0
0 0.5000 0
0 0 0.3333
>> A/B - A*B^(-1) % division considered as multiplication with inverse
ans =
0 0 0
0 0 0
>> B\At - B^(-1)*At
ans =
0 0
0 0
0 0
If you want to use addition, subtraction, multiplication and exponentiation in components of vector or matrix elements, then place a point before the operator. We give some examples.
Operations on matrix elements
>> A
A =
1 2 3
4 5 6
>> A .^ 2
ans =
1 4 9
16 25 36
>> A .* A
ans =
1 4 9
16 25 36
>> A ./ A
ans =
1 1 1
1 1 1
Unlock full access