Professional Documents
Culture Documents
E7 D5 E7 D5
3 4
Stacking 4 matrices - Hints Stacking 4 matrices - Hints
• Determine the dimensions of Ā, B̄, C̄, D̄ • Insert the matrices A, B, C, D into Ā, B̄, C̄, D̄
ma = mc mb = md [na,ma]=size(A)!
2 3
1 1 1 1 0
Ā = 1 1 1 B̄ =4 2 5 0 na = nb
0 0 0 3 0 • Stack the matrices Ā, B̄, C̄, D̄ to create E
⇥ ⇤
1 2 3 4 4
C̄ = D̄ = nc = nd
0 0 0 4 4
2 2 3
3
1 1 1 1 0
6 1 1 1 4 2 50 7
• Initialize the matrices Ā, B̄, C̄, D̄ 6 7
E=6
6⇥
0 0 0 3 0
⇤
7
7
4 1 2 3 4 4 5
0 0 0 4 4
Ab = zeros(nab,mab)!
E7 D5 E7 D5
5 6
Part B - Matlab Polynomial representation Adding two polynomial
Examples:
p(x) = 6x3 + 5x2 – 3x + 7
p(x) = 6x3 + 5x2 – 3x + 7
q(x) = 9x2 – 2x + 4
>> p = [ 6 5 -3 7 ];
p(x) + q(x) = 6x3 + 14x2 – 5x + 11
q(x) = 9x2 – 2x + 4
>> p = [ 6 5 -3 7 ];
>> q = [ 9 -2 4]; >> q = [ 0 9 -2 4];
>> p + q = [ 6 14 -5 11];
>> q = [ 0 9 -2 4];
E7 L3 E7 L3
7 8
Example of empty numeric arrays Padding a row vector with zeros to the left
• Concatenate empty and
>> a = []
a =
non-empty arrays Given:
[] >> v = [1 2 3 ];
>> w1 = [a , v]
>> q = [ 9 -2 4];
>> b = ones(0,1)
w1 =
b =
1 2 3
Empty matrix: 0-by-1 length N = 3
>> w2 = [b , v]
w2 =
>> c = ones(1,-3) 1 2 3 How do we generate?
c =
• Assign an empty 0 x 0 to
Empty matrix: 1-by-0
an element of a vector
>> w2
q_pad = [ 0 0 0 9 -2 4];
w2 =
1 2 3
length M = 6
>> w2(2) = []
w2 =
1 3
E7 D5 E7 D5
9 10
Padding a row vector with zeros to the left Padding a row vector with zeros to the left
>> q1 = zeros(1,M-length(q))
>> q1 = zeros(1,M-length(q))
q1 = <0
q1 =
0 0 0
Empty matrix: 1-by-0
>> q_pad = [ q1 , q ]
>> q_pad = [ q1 , q ]
qp = qp =
0 0 0 9 -2 4 9 -2 4
E7 D5 E7 D5
11
Part C – Create a Cell array from two other arrays
%% create a cell array
• Consider a double array SG and a cell array SN
air = { 0.21 'oxygen' 32 'O2'
0.78 'nitrogen' 28 'N2'
0.01 'argon' 40 'Ar' }
>> sortrows(air,1)
ans =
column index
[0.01] 'argon' [40] 'Ar'
[0.21] 'oxygen' [32] 'O2'
[0.78] 'nitrogen' [28] 'N2'
SID1=SG(:,1)! SID2=[SN{:,1}]’! >> sortrows(air,2)
ans =
SID1 may be different from SID2, unless the rows [0.01]
[0.78]
'argon'
'nitrogen'
[40]
[28]
'Ar'
'N2'
of both SG and SN are sorted first (ascending order)! [0.21] 'oxygen' [32] 'O2'
>> sortrows(air,3)
ans =
[0.78] 'nitrogen' [28] 'N2'
[0.21] 'oxygen' [32] 'O2'
[0.01] 'argon' [40] 'Ar'
E7 D5
13 14
Part C – Create a Cell array from two other arrays Part D – Create a struct array form a cell array
• Given the sorted double array SG1 and cell array SN1
• Given the cell array RP
CS.SID(i) = RP{i, 1}
CS.name(i) = RP{i, 2}
CS.G(i) = RP{i, 3}
CS.P(i) = RP{i, 4}
15 16
Part D – Create a struct array form cell arrays Part D – Create a struct array form a cell array
• Given the cell array R Create the cell array RP from the cell array R