Matlab MatrixArray Guide
Matlab MatrixArray Guide
A = 1×3
1 2 3
% To make matrices with several rows the ";" can be used to transition to
% next row
A2 = 2×2
1 2
4 5
reshape( A2, [1,4]) % resized matrix must have same no. of entries as original
ans = 1×4
1 4 2 5
% row matrices can also be turned into column matrices using '. consider A5
A'
ans = 3×1
1
2
3
ans = 2×2
-1.6667 0.6667
1.3333 -0.3333
1
Matrix Operations
A+10
ans = 1×3
11 12 13
sin(A)
ans = 1×3
0.8415 0.9093 0.1411
M1 = [ 1 2 3 ; 4 5 6]
M1 = 2×3
1 2 3
4 5 6
M2 = [ 1 2 ; 3 4 ; 5 6]
M2 = 3×2
1 2
3 4
5 6
M3 = [ 6 7 8 ; 8 7 6]
M3 = 2×3
6 7 8
8 7 6
% regular matrix operations use regular operators, but they must have
% compatible dimensions M1/M3 and M2 are incompatible for addition and
% substration
M1 + M3
ans = 2×3
2
7 9 11
12 12 12
M1 - M3
ans = 2×3
-5 -5 -5
-4 -2 0
M1.*M3
ans = 2×3
6 14 24
32 35 36
M1./M3
ans = 2×3
0.1667 0.2857 0.3750
0.5000 0.7143 1.0000
ans = 2×3
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
ans = 2×3
1 4 9
16 25 36
3
% to create an array that has entries with a specific
% : is used, for example
1 : 2 : 10 % creates a 1x5 array starting with 1 with a difference of 2 between elements upto a
ans = 1×5
1 3 5 7 9
linspace(2,0.5,4) % creates an array with 4 elements, starting value of 2 end value of 0.5
ans = 1×4
2.0000 1.5000 1.0000 0.5000
%maximun and minimum values in a array can be found using min and
%max operators. Consider array V
V = [ 44 54 23 1 67]
V = 1×5
44 54 23 1 67
max(V)
ans = 67
min(V)
ans = 1
P = [ 1 2 3 ; 5 6 7 ; 9 10 11 ] % a 3x3 matrix
4
P = 3×3
1 2 3
5 6 7
9 10 11
P(3,2)
ans = 10
% method 2; using single subscript that goes down from first element of
% leftmost column called linear indexing
P(6)
ans = 10
P(4,4) = 16
P = 4×4
1 2 3 0
5 6 7 0
9 10 11 0
0 0 0 16
ans = 3×1
2
6
10
ans = 1×2
2 3
% a : alone in the place of a row or column yields all the elements of that
% row/column, for example P(:,:) yeilds the original matrix.
5
6