You are on page 1of 4

1 2

Part A - Stacking 4 matrices of various dimensions


E7: INTRODUCTION TO COMPUTER
PROGRAMMING FOR SCIENTISTS AND •  Consider
ENGINEERS  1
3

2


1 1 1 4 4
A= B=4 2 5 C= 1 2 3 D=
1 1 1 4 4
3
Discussion Session September 21, 2012
•  We want to insert these matrices into other matrices, so that
we can stack them into a single matrix E as follows:
•  Part A – stacking matrices
 2
3 2 3
•  Part B – adding polynomials 1 1 1 1 0 
1 1 1
2
1 0
3
Ā = 1 1 1 B̄ =4 2 5 0 6 7
•  Part C – create and manipulate cell arrays 0 0 0 3 0 6 1 1 1 4 2 50
7
•  Part D – create structure arrays E=6
6⇥
0 0 0 3 0 7
⇤ 7
⇥ ⇤  4 1 2 3 4 4 5
1 2 3 4 4 0 0 0 4 4
C̄ = D̄ =
Copyright 2007, Horowitz, Packard. This work is licensed under the Creative Commons Attribution-Share 0 0 0 4 4
Alike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a
letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

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

Idea: What happens if M ≤ N ?


>> M = 6; >> M = 2;
>> q = [ 9 -2 4]; (length N = 3) >> q = [ 9 -2 4]; (length N = 3)

>> 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

•  Create a 1 dimensional struct array CS with 4 fields:

CS.SID(i) = RP{i, 1}
CS.name(i) = RP{i, 2}
CS.G(i) = RP{i, 3}
CS.P(i) = RP{i, 4}

•  create the cell array R CS = cell2struct(RP,{'SID','name','G','P'},2)!


use num2cell !
and cell array concatenation field names
E7 D5 E7 D5

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

•  Create the cell array RP

•  “Grab” the 3 column of R and create the double column G

•  Create the double column P from G!


using the >= operator
8
< 0 if RP{i, 3} < 70
RP{i, 4} = •  Create a column cell array PC from the double column P
: 1 if RP{i, 3} 70 •  Concatenate the cell arrays R and PC
E7 D5 E7 D5

You might also like