Post on 29-Dec-2015
Lesson 4
Arrays in MATLAB First round Lecturer : Dr. Noam Amir
Format Revised. 01-11-2007
CE2002-NTUST
Outline:
Testing arrays in MATLAB The colon operator Concatenating arrays in MATLAB Array indexing Array math Sorting and searching in arrays
Working with Matrices
MATLAB == MATrix LABoratory
>> load durer >> image(X) >> colormap(map)
>> load detail >> image(X) >> colormap(map)
Every variable is an array:
>> a=1;
>> size(a) ans = 1 1
>> length(a) ans = 1
>> a=1;
>> size(a) ans = 1 1
>> length(a) ans = 1
Even a scalar
Checking its dimensions:
Checking on the number of elements:
>> a=[1 2 3]
a=
1 2 3
>> size(a) ans = 1 3
>> length(a) ans = 3
>> a=[1 2 3]
a=
1 2 3
>> size(a) ans = 1 3
>> length(a) ans = 3
A vector can be a row or a column:
>> a=[1; 2; 3]
a=
1
2
3
>> size(a) ans = 3 1
>> length(a) ans = 3
>> a=[1; 2; 3]
a=
1
2
3
>> size(a) ans = 3 1
>> length(a) ans = 3 row
column
Entering Numeric Arrays
>> a=[1 2;3 4]
a =
1 2
3 4
>> b = 2:-0.5:0 b = 2 1.5 1 0.5 0
>> c = rand(2,4) c = 0.8913 0.45647 0.82141 0.61543 0.7621 0.018504 0.4447 0.79194
>> a=[1 2;3 4]
a =
1 2
3 4
>> b = 2:-0.5:0 b = 2 1.5 1 0.5 0
>> c = rand(2,4) c = 0.8913 0.45647 0.82141 0.61543 0.7621 0.018504 0.4447 0.79194
Row separator: semicolon (;) Column separator:space / comma (,)
Use square brackets [ ]
Matrices must be rectangular. (Undefined elements set to zero)
Creating sequences using the colon operator (:)
Utility function for creating matrices. (Ref: Utility Commands)
Entering Numeric Arrays - cont.
>> w = [-2.8, sqrt(-7), (3+5+6)*3/4] w = -2.8 0 + 2.6458i 10.5 >> x(3,2) = 3.5 x = 0 0 0 0 0 3.5
>> w(2,5) = 23
w = -2.8 0 + 2.6458i 10.5 0 0 0 0 0 0 23
>> w = [-2.8, sqrt(-7), (3+5+6)*3/4] w = -2.8 0 + 2.6458i 10.5 >> x(3,2) = 3.5 x = 0 0 0 0 0 3.5
>> w(2,5) = 23
w = -2.8 0 + 2.6458i 10.5 0 0 0 0 0 0 23
Using other MATLAB expressions
Matrix element assignment
Any MATLAB expression can be entered as a matrix element
Some more about the colon (:) :
>> 1:5 ans = 1 2 3 4 5 >> a=5:1 a = Empty matrix: 1-by-0
>> 1:0.3:2
ans = 1.0000 1.3000 1.6000 1.9000
>> 1:5 ans = 1 2 3 4 5 >> a=5:1 a = Empty matrix: 1-by-0
>> 1:0.3:2
ans = 1.0000 1.3000 1.6000 1.9000
Default step is 1
Impossible values are not an error
If upper limit is not attainable – the values stop before it
Alternatives to the colon
>> linspace(0,pi,5) ans= 0 0.7854 1.5708 2.3562 3.1416 >> logspace(0,2,3) ans= 1 10 100 >> logspace(0,1,10) ans= Columns 1 through 6 1.0000 1.2915 1.6681 2.1544 2.7826 3.5938 Columns 7 through 10 4.6416 5.9948 7.7426 10.0000
>> linspace(0,pi,5) ans= 0 0.7854 1.5708 2.3562 3.1416 >> logspace(0,2,3) ans= 1 10 100 >> logspace(0,1,10) ans= Columns 1 through 6 1.0000 1.2915 1.6681 2.1544 2.7826 3.5938 Columns 7 through 10 4.6416 5.9948 7.7426 10.0000
• linspace and logspace:
• linspace(first,last,N)
• logspace(first_exp,last_exp,N)
Numerical Array Concatenation - [ ]
>> a=[1 2;3 4]
a =
1 2
3 4
>> cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24
>> a=[1 2;3 4]
a =
1 2
3 4
>> cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24
Use [ ] to combine existing arrays as matrix “elements”
Use square brackets [ ]
4*a
Row separator: semicolon (;) Column separator:space / comma (,)
Matrices must be rectangular.
Indexing into a vector:
>> a=5:10 a = 5 6 7 8 9 10 >> a(1) ans= 5 >> a(2:4) ans= 6 7 8 >> a([1 3 2]) ans= 5 7 6
>> a=5:10 a = 5 6 7 8 9 10 >> a(1) ans= 5 >> a(2:4) ans= 6 7 8 >> a([1 3 2]) ans= 5 7 6
• Indexing is done with parentheses
• A vector of indices is permissible
• Indexing is similar for row or column vectors
>> a=(5:10)’ a = 5 6 7 8 9 10 >> a(1) ans= 5 >> a(2:4) ans= 6 7 8 >> a([1 3 2]) ans= 5 7 6
>> a=(5:10)’ a = 5 6 7 8 9 10 >> a(1) ans= 5 >> a(2:4) ans= 6 7 8 >> a([1 3 2]) ans= 5 7 6
Transpose
A matrix has more than one dimension - So an index into a matrix has to address each dimension
separately, e.g.: mat(3,5)rowcolumn
BUT: After all, a matrix is stored in memory as a string of
elements And we can address that string of elements as a
VECTOR! In other words – matrices in Matlab have schizophrenia…
let’s have a look:
Indexing into a matrix:
Indexing the Matrix in MATLAB
4 10 1 6 2
8 1.2 9 4 25
7.2 5 7 1 11
0 0.5 4 5 56
23 83 13 0 10
1 2 Rows (m) 3 4
5
Columns (n)
1 2 3 4 51 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25
A =A (2,4)
A (17)
Rectangular Matrix: Scalar: 1-by-1 array Vector: m-by-1 array
1-by-n array Matrix: m-by-n array
Matrix elements can be EITHER numbers OR characters
Array Subscripting / Indexing
4 10 1 6 2
8 1.2 9 4 25
7.2 5 7 1 11
0 0.5 4 5 56
23 83 13 0 10
1 2 3 4 5
1 2 3 4 51 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25
A =
A(3,1) A(3)
A(1:5,5) A(:,5) A(21:25)
A(4:5,2:3) A([9 14;10 15])
• Use () parentheses to specify index • colon operator (:) specifies range / ALL • [ ] to create matrix of index subscripts • 'end' specifies maximum index value
A(1:end,end) A(:,end) A(21:end)’
Matrix examples:
>> a=[1 2;3 4] a = 1 2 3 4 >> a(1,4)=5 % auto enlarging a = 1 2 0 5 3 4 0 0 >> a(:,3)=[4; 4] a = 1 2 4 5 3 4 4 0 >> b=a(:,4:-1:1) b = 5 4 2 1 0 4 4 3
>> a=[1 2;3 4] a = 1 2 3 4 >> a(1,4)=5 % auto enlarging a = 1 2 0 5 3 4 0 0 >> a(:,3)=[4; 4] a = 1 2 4 5 3 4 4 0 >> b=a(:,4:-1:1) b = 5 4 2 1 0 4 4 3
>> b(6) % columnwise unraveling
ans =
4
>> b(:) % columnwise unraveling
ans =
5
0
4
4
2
4
1
3
>> b(1,:)=[] % erasing a row
b =
0 4 4 3
>> b(6) % columnwise unraveling
ans =
4
>> b(:) % columnwise unraveling
ans =
5
0
4
4
2
4
1
3
>> b(1,:)=[] % erasing a row
b =
0 4 4 3
Matrices as vectors and vice versa:
>> a=[1 2 3; 4 5 6]
a =
1 2 3
4 5 6
>> a(1:2)
ans =
1 4
>> a(5)
ans =
3
>> a(end)
ans =
6
>> a=[1 2 3; 4 5 6]
a =
1 2 3
4 5 6
>> a(1:2)
ans =
1 4
>> a(5)
ans =
3
>> a(end)
ans =
6
• The previous pages showed that we can index into a matrix like a vector
• Unraveling is columnwise
>> sub2ind(size(a),2,3)
ans =
6
>> [r,c]=ind2sub(size(a),4)
r =
2
c =
2
>> sub2ind(size(a),2,3)
ans =
6
>> [r,c]=ind2sub(size(a),4)
r =
2
c =
2
• 2 functions translate between the two types of indices:
Logical indexing:
>> a=-3:3 a = -3 -2 -1 0 1 2 3 >> abs(a)>1 ans = 1 1 0 0 0 1 1 >> a(abs(a)>1) ans = -3 -2 2 3
>> a=-3:3 a = -3 -2 -1 0 1 2 3 >> abs(a)>1 ans = 1 1 0 0 0 1 1 >> a(abs(a)>1) ans = -3 -2 2 3
• Logical operations give 0 or 1
• The result of such operations is alogical array
• This kind of array can be used similarly to indexes - to retain or eliminate elements of a vector:
Logical indexing – cont.:
>> a([1 1 0 0 0 1 1]) % ouch!
??? Subscript indices must either be real positive integers or logicals.
>> a(logical([1 1 0 0 0 1 1]))
ans =
-3 -2 2 3
>> a([1 1 0 0 0 1 1]) % ouch!
??? Subscript indices must either be real positive integers or logicals.
>> a(logical([1 1 0 0 0 1 1]))
ans =
-3 -2 2 3
• An array that is composed of 1’s and 0’s isn’t necessarily a logical array
• A numeric array of 1 and 0 can be converted into a logical array using: logical()
Standard matrices:
• Some useful matrices can be created more easily:
>> ones(2) ans = 1 1 1 1 >> ones(2,3) ans= 1 1 1 1 1 1 >> rand(1,4) ans= 0.4565 0.0185 0.8214 0.4447 >> diag([ 1 2 3]) ans = 1 0 0 0 2 0 0 0 3
>> ones(2) ans = 1 1 1 1 >> ones(2,3) ans= 1 1 1 1 1 1 >> rand(1,4) ans= 0.4565 0.0185 0.8214 0.4447 >> diag([ 1 2 3]) ans = 1 0 0 0 2 0 0 0 3
Makes a diagonal matrix from a vector
diag
Random numbers, gaussian distribution, zero mean, unit var.
randn
Random numbers, uniform distribution on [0,1]
rand
Identity matrixeye
All 0zeros
All 1ones
Size and length again:
>> a=ones(3,4); >> size(a) ans = 3 4 >> size(a,1) ans = 3 >> size(a,2) ans = 4
>> a=ones(3,4); >> size(a) ans = 3 4 >> size(a,1) ans = 3 >> size(a,2) ans = 4
• size gives a vector
• But we can obtain one element from it:
>> a=ones(1,5); % row
>> length(a)
ans =
5
>> a=ones(5,1); % column
>> length(a)
ans =
5
>> a=ones(5,3,4); % 3D
>> length(a)
ans =
5
>> a=ones(1,5); % row
>> length(a)
ans =
5
>> a=ones(5,1); % column
>> length(a)
ans =
5
>> a=ones(5,3,4); % 3D
>> length(a)
ans =
5
• length gives the length of a vector…
• But the maximum dimension of a matrix
Empty arrays:
>> a=ones(3,4); >> numel(a) ans = 12 >> a=[] % empty variable a = [] >> numel(a) ans = 0 >> size(a) ans = 0 0
>> a=ones(3,4); >> numel(a) ans = 12 >> a=[] % empty variable a = [] >> numel(a) ans = 0 >> size(a) ans = 0 0
• numel gives the number of elements in an array:
>> a=zeros(0,3)
a =
Empty matrix: 0-by-3
>> size(a)
ans =
0 3
>> length(a)
ans =
0
>> numel(a)
ans =
0
>> a(1,:)=[1 2 3]
a =
1 2 3
>> a=zeros(0,3)
a =
Empty matrix: 0-by-3
>> size(a)
ans =
0 3
>> length(a)
ans =
0
>> numel(a)
ans =
0
>> a(1,:)=[1 2 3]
a =
1 2 3
• BUT- an empty matrix can have a nonzero dimension:
Rearranging the dimensions:
>> a=1:10; % 10 elements >> reshape(a,2,5) % 10 elements! ans = 1 3 5 7 9 2 4 6 8 10 >> reshape(a,5,2) % 10 elements! ans = 1 6 2 7 3 8 4 9 5 10
>> a=1:10; % 10 elements >> reshape(a,2,5) % 10 elements! ans = 1 3 5 7 9 2 4 6 8 10 >> reshape(a,5,2) % 10 elements! ans = 1 6 2 7 3 8 4 9 5 10
• reshape takes the elements of an array and rearranges with new dimensions:
>> a=[1 2;3 4];
>> repmat(a,1,2)
ans =
1 2 1 2
3 4 3 4
>> repmat(a,[1 2]) % same
ans =
1 2 1 2
3 4 3 4
>> repmat(a,2) % ‘2’ is extended
ans =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
>> a=[1 2;3 4];
>> repmat(a,1,2)
ans =
1 2 1 2
3 4 3 4
>> repmat(a,[1 2]) % same
ans =
1 2 1 2
3 4 3 4
>> repmat(a,2) % ‘2’ is extended
ans =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4
• repmat repeats an array
A word about transposing:
The operator for transposing is the apostrophe (‘) Complex numbers
are conjugated!
For non-conjugating transpose, use: (.’)
>> a=(5:7)+j a = 5.0000 + 1.0000i 6.0000 + 1.0000i
7.0000 + 1.0000i >> a’ ans= 5.0000 - 1.0000i 6.0000 - 1.0000i 7.0000 - 1.0000i >> a.’ ans= 5.0000 + 1.0000i 6.0000 + 1.0000i 7.0000 + 1.0000i
>> a=(5:7)+j a = 5.0000 + 1.0000i 6.0000 + 1.0000i
7.0000 + 1.0000i >> a’ ans= 5.0000 - 1.0000i 6.0000 - 1.0000i 7.0000 - 1.0000i >> a.’ ans= 5.0000 + 1.0000i 6.0000 + 1.0000i 7.0000 + 1.0000i
Array sorting Matrices: each column is sorted separately:
>> x=randperm(6) x = 2 4 3 6
5 1 >> xs=sort(x) %ascending xs = 1 2 3 4
5 6 >> [xs,ixs]=sort(x)
%ascending xs = 1 2 3 4
5 6 ixs = 6 1 3 2
5 4
>> x=randperm(6) x = 2 4 3 6
5 1 >> xs=sort(x) %ascending xs = 1 2 3 4
5 6 >> [xs,ixs]=sort(x)
%ascending xs = 1 2 3 4
5 6 ixs = 6 1 3 2
5 4
• Vectors:
>> m=[randperm(4);randperm(4);randperm(4)]
m =
1 4 3 2
2 3 4 1
1 2 4 3
>> [sm,ism]=sort(m)
sm =
1 2 3 1
1 3 4 2
2 4 4 3
ism =
1 3 1 2
3 2 2 1
2 1 3 3
>> m=[randperm(4);randperm(4);randperm(4)]
m =
1 4 3 2
2 3 4 1
1 2 4 3
>> [sm,ism]=sort(m)
sm =
1 2 3 1
1 3 4 2
2 4 4 3
ism =
1 3 1 2
3 2 2 1
2 1 3 3
Note: randperm(n) gives a random permutationof the integers from 1 to n
Array sorting – contd.
>> m
m =
1 4 3 2
2 3 4 1
1 2 4 3
>> [tmp,ind]=sort(m(:,2));
>> % based on 2'nd column
>> m(ind,:)
ans =
1 2 4 3
2 3 4 1
1 4 3 2
>> m
m =
1 4 3 2
2 3 4 1
1 2 4 3
>> [tmp,ind]=sort(m(:,2));
>> % based on 2'nd column
>> m(ind,:)
ans =
1 2 4 3
2 3 4 1
1 4 3 2
• Sorting all columns based on only one:
>> [sm,ism]=sort(m,2) % add a dimension
sm =
1 2 3 4
1 2 3 4
1 2 3 4
ism =
1 4 3 2
4 1 2 3
1 2 4 3
>> [sm,ism]=sort(m,2) % add a dimension
sm =
1 2 3 4
1 2 3 4
1 2 3 4
ism =
1 4 3 2
4 1 2 3
1 2 4 3
• Sort rows:
Searching in a vector:
>> x=-3:3
x =
-3 -2 -1 0 1 2 3
>> k=find(abs(x)>1) % find indices where abs(x)>1
k =
1 2 6 7
>> x(k) % extract those values
ans =
-3 -2 2 3
>> x(abs(x)>1) % logical addressing does the same thing
ans =
-3 -2 2 3
>> x=-3:3
x =
-3 -2 -1 0 1 2 3
>> k=find(abs(x)>1) % find indices where abs(x)>1
k =
1 2 6 7
>> x(k) % extract those values
ans =
-3 -2 2 3
>> x(abs(x)>1) % logical addressing does the same thing
ans =
-3 -2 2 3
• Searching in a vector is simple - find
Searching in a matrix:
>> m=[1 2 3 4;5 6 7 8]
m =
1 2 3 4
5 6 7 8
>> [ii,jj]=find(m>3)%one way
ii =
2
2
2
1
2
jj =
1
2
3
4
4
>> m=[1 2 3 4;5 6 7 8]
m =
1 2 3 4
5 6 7 8
>> [ii,jj]=find(m>3)%one way
ii =
2
2
2
1
2
jj =
1
2
3
4
4
• Same command - find >> ind=find(m>3)% another way
ind =
2
4
6
7
8
>> m(ind)=0 %replace with zeros
m =
1 2 3 0
0 0 0 0
>> m(ii,jj) % ouch!!
ans =
5 6 7 8 8
5 6 7 8 8
5 6 7 8 8
1 2 3 4 4
5 6 7 8 8
>> ind=find(m>3)% another way
ind =
2
4
6
7
8
>> m(ind)=0 %replace with zeros
m =
1 2 3 0
0 0 0 0
>> m(ii,jj) % ouch!!
ans =
5 6 7 8 8
5 6 7 8 8
5 6 7 8 8
1 2 3 4 4
5 6 7 8 8
maximum and minimum in a vector:>> v=rand(1,6)
v =
0.6038 0.2722 0.1988 0.0153 0.7468 0.4451
>> max(v) % maximum
ans =
0.7468
>> [mx,ind]=max(v) % maximum and index
mx =
0.7468
ind =
5
>> min(v) % minimum
ans =
0.0153
>> [mn,ind]=min(v) % minimum and index
mn =
0.0153
ind =
4
>> v=rand(1,6)
v =
0.6038 0.2722 0.1988 0.0153 0.7468 0.4451
>> max(v) % maximum
ans =
0.7468
>> [mx,ind]=max(v) % maximum and index
mx =
0.7468
ind =
5
>> min(v) % minimum
ans =
0.0153
>> [mn,ind]=min(v) % minimum and index
mn =
0.0153
ind =
4
maximum and minimum in a matrix:>> m=rand(3,6) % maximum per column
m =
0.9318 0.8462 0.6721 0.6813 0.5028 0.3046
0.4660 0.5252 0.8381 0.3795 0.7095 0.1897
0.4186 0.2026 0.0196 0.8318 0.4289 0.1934
>> [mx,r]=max(m)
mx =
0.9318 0.8462 0.8381 0.8318 0.7095 0.3046
r =
1 1 2 3 2 1
>> max(max(m)) % overall maximum – bad way
ans =
0.9318
>> [mmx,ii]=max(m(:)) % overall maximum – good way, with index!
mmx =
0.9318
ii =
1
>> m=rand(3,6) % maximum per column
m =
0.9318 0.8462 0.6721 0.6813 0.5028 0.3046
0.4660 0.5252 0.8381 0.3795 0.7095 0.1897
0.4186 0.2026 0.0196 0.8318 0.4289 0.1934
>> [mx,r]=max(m)
mx =
0.9318 0.8462 0.8381 0.8318 0.7095 0.3046
r =
1 1 2 3 2 1
>> max(max(m)) % overall maximum – bad way
ans =
0.9318
>> [mmx,ii]=max(m(:)) % overall maximum – good way, with index!
mmx =
0.9318
ii =
1
Further manipulations:
If any dimension is 1 – dimension is removedsqueeze
Extract lower diagonal from matrix (zero all that is above diagonal)
tril
Extract upper diagonal from matrix (zero all that is below diagonal)
triu
Extract diagonal from matrixdiag(m)
Make vector into a diagonal matrixdiag(vec)
Rotate 90 degrees twice counterclockwiserot(m,2)
Rotate 90 degrees counterclockwiserot90
Flip array in left-right directionfliplr
Flip array in up-down directionflipud
Array math in Matlab Linear algebra vs. simple algebra
When performing mathematical operations between arrays in Matlab – how do we determine what rules will be used? Matlab has a set of rules that is generally simple and easy to follow Let’s examine the possible cases:
Scalar/Matrix math:
>> w=[1 2;3 4] + 5 w = 6 7 8 9
>> w=[1 2;3 4] + 5 w = 6 7 8 9
Scalar expansion
>> w=[1 2;3 4] + 5 1 2 = + 5 3 4 1 2 5 5 = + 3 4 5 5 6 7 = 8 9
>> w=[1 2;3 4] + 5 1 2 = + 5 3 4 1 2 5 5 = + 3 4 5 5 6 7 = 8 9
Generally this works for + - * / ^ …
Similarly:
>> w=[1 2;3 4] * 5 w = 6 7 8 9
>> w=[1 2;3 4] * 5 w = 6 7 8 9
>> w=[1 2;3 4] / 5 w = 0.2 0.4 0.6 0.8
>> w=[1 2;3 4] / 5 w = 0.2 0.4 0.6 0.8
>> w=5 * [1 2;3 4] w = 6 7 8 9
>> w=5 * [1 2;3 4] w = 6 7 8 9
BUT:>> w=5 / [1 2;3 4] ??? Error using ==> mrdivide Matrix dimensions must agree.
>> w=5 / [1 2;3 4] ??? Error using ==> mrdivide Matrix dimensions must agree.
To resolve this we have to go a bit further…
Simple Matrix/Matrix math:
>> w=[1 2;3 4] + [5 5;5 5]
6 7 = 8 9
>> w=[1 2;3 4] .* [5 5;5 5]
5 10 = 15 20
>> w=[1 2;3 4] + [5 5;5 5]
6 7 = 8 9
>> w=[1 2;3 4] .* [5 5;5 5]
5 10 = 15 20
• Performing calculations element by element:
• Matrices must be the same size
• With + and – there is no ambiguity
• With other operators – add a dot: .* ./ .^ etc.
>> w=5 ./ [1 2;3 4] 5 2.5
1.6667 1.25
>> w=5 ./ [1 2;3 4] 5 2.5
1.6667 1.25
… and to address the problem on the previous slide:
In this case:
Matrices must have the same dimensions imensions of resulting matrix = dimensions of
multiplied matrices Resulting elements = product of corresponding
elements from the original matrices Same rules apply for other array operations
>> a = [1 2 3 4; 5 6 7 8];
>> b = [1:4; 1:4];
>> c = a.*b
c =
1 4 9 16 5 12 21 32
>> a = [1 2 3 4; 5 6 7 8];
>> b = [1:4; 1:4];
>> c = a.*b
c =
1 4 9 16 5 12 21 32
c(2,4) = a(2,4)*b(2,4)
NEXT:Matrix Multiplication and Division
Multiplication: Inner dimensions must be equal. Dimension of resulting matrix = outermost dimensions of multiplied matrices. Resulting elements = dot product of the rows of the 1st matrix with the columns of the 2nd matrix.
>> a = [1 2 3 4; 5 6 7 8];
>> b = ones(4,3);
>> c = a*b
c =
10 10 10 26 26 26
>> a = [1 2 3 4; 5 6 7 8];
>> b = ones(4,3);
>> c = a*b
c =
10 10 10 26 26 26
[2x4]
[4x3]
[2x4]*[4x3] [2x3]
a(2nd row).b(3rd column)
Division (solving equations):using “Left Division”
To Solve the set of simultaneous equations:
we need to calculate:
>> A = [-1 1 2; 3 -1 1;-1 3 4];
>> b = [2;6;4];
>> x = inv(A)*b
x =
1.0000
-1.0000
2.0000
>> x = A\b
x =
1.0000
-1.0000
2.0000
>> A = [-1 1 2; 3 -1 1;-1 3 4];
>> b = [2;6;4];
>> x = inv(A)*b
x =
1.0000
-1.0000
2.0000
>> x = A\b
x =
1.0000
-1.0000
2.0000
-x1 + x2 + 2x3 = 2
3x1 - x2 + x3 = 6
-x1 + 3x2 + 4x3 = 4
bAx 1 x b A
Some Matrix and Array Operators:
>> help ops >> help matfun
(In order of precedence)
Matrix Operators Array operators
() parentheses
’ comp. transpose .’ array transpose
^ power .^ array power
* multiplication .* array mult.
/ division ./ array division
\ left division
+ addition
- subtraction
Common Matrix Functions inv matrix inverse
det determinant
rank matrix rank
eig eigenvectors & values
svd singular value dec.
norm matrix / vector norm
• In most languages – you have to use loops:
•
•
•
•
• In MATLAB - use Array Operations instead:
•
•
•
• This kind of code is shorter and simpler
IMPORTANT Example: Array Operations
>> tic; for I = 1:10000 Density(I) = Mass(I)/(Length(I)*Width(I)*Height(I)); end; toc elapsed_time = 4.7260
>> tic; for I = 1:10000 Density(I) = Mass(I)/(Length(I)*Width(I)*Height(I)); end; toc elapsed_time = 4.7260
>> tic; Density = Mass./(Length.*Width.*Height); toc
elapsed_time =
0
>> tic; Density = Mass./(Length.*Width.*Height); toc
elapsed_time =
0
Use TIC and TOC to measure elapsed time
Vectorized code WAS much faster than loops
Before JIT!
Another example of loopless vs loopy code:
Successive differences: Cumulative sum:
>> a=1:10;
>> for iii=1:9
b(iii)=a(iii+1)-a(iii);
end >> b b = 1 1 1 1 1 1 1 1 1
>> a=1:10;
>> for iii=1:9
b(iii)=a(iii+1)-a(iii);
end >> b b = 1 1 1 1 1 1 1 1 1
>> diff(a) ans = 1 1 1 1 1 1 1 1 1
>> diff(a) ans = 1 1 1 1 1 1 1 1 1
>> cumsum(b) ans = 1 2 3 4 5 6 7 8 9
>> cumsum(b) ans = 1 2 3 4 5 6 7 8 9
loopedloopless
More about programming with arrays in a later lesson!
Summary:
Arrays are very easy to use in MATLAB No declarations are necessary, and array dimensions can be modified dynamically Manipulating and indexing into arrays is very powerful, and can be complicated at times Arrays can be sorted and searched easily Math with arrays can be performed either element by element, or as defined by the rules of linear algebra