Title: L16.%20Cell%20Arrays
1L16. Cell Arrays
- Set-Up
- Subscripting
- Nested Loops
- String Manipulations
2A Small Cell Array
C Alabama,New York,Utah
C
3Syntax
Entries Separated by Commas
C Alabama,New York,Utah
Curly Brackets
4Synonym
C Alabama,New York,Utah
C cell(1,3) C1 Alabama C2 New
York C3 Utah
Application Storing strings
5Vertical Cell Array Set-up
C AlabamaNew YorkUtah
C cell(3,1) C1 Alabama C2 New
York C3 Utah
Application Storing strings
6Another Small Cell Array
C 1 2 3,1020, zeros(1,4)
C
7Syntax
Entries Separated by Commas
C 1 2 3, 1020, zeros(1,4)
Curly Brackets
8Synonym
C 1 2 3, 1020, zeros(1,4)
C cell(1,3) C1 1 2 3 C2
1020 C3 zeros(1,4)
Application Storing a Set of Arrays
9Problem Set Up a Card Deck
10Idea
- A1 A Hearts
- A2 2 Hearts
-
- A13 K Hearts
- A14 A Clubs
-
- A52 K Diamonds
11Initializations
- suit Hearts, Clubs,
- Spades, Diamonds
- rank A,2,3,4,5,6,
- 7,8,9,10,J,Q,K
- A cell(1,52)
12Use Concatenation
- suit Hearts, Clubs,
- Spades, Diamonds
- rank A,2,3,4,5,6,
- 7,8,9,10,J,Q,K
- A16 rank3 suit2
A16 3 Clubs
13Nested Loop to Get allPossible Combinations
- i is index of next card
- i 1
- for k14
- Set up the cards in suit k
- for j113
- Ai rankj ' ' suitk
- i i1
- end
- end
14Problem Deal a Card Deck
15Deal a length-12 Card Deck
A
4k-3
E
2,6,10
4k-2
S
3,7,11
4k-1
4k
W
4,8,12
16- N cell(1,13) E cell(1,13)
- S cell(1,13) W cell(1,13)
-
- for k113
- Nk A4k-3
- Ek A4k-2
- Sk A4k-1
- Wk A4k
- end
17Problem Shuffle a Card Deck
18Shuffle a length-12 Card Deck
19Step 1 Cut the Deck
20Step 2 Alternate
1 2 3 4 5 6
1 2 3 4 5 6 7 8 9 10 11 12
21Step 2 Alternate
1 2 3 4 5 6
k -gt 2k-1
1 3 5 7 9 11
22Step 2 Alternate
1 2 3 4 5 6
k -gt 2k
2 4 6 8 10 12
23- function T Shuffle(S)
- n length(S) m n/2
- T cell(n,1)
- Top S(1m) Bot S(m1n)
- for k1m
- T2k-1 Topk
- T2k Botk
- end
248 Shuffles with a Card Deck
- And you are back where you started.
25Illustrate with Color
Set up a 52-color spectrum C cell(52,1) for
k152 f (k-1)/51 Ck f 0
1-f end
These are colors
26Using fill( , , Ck)
8
7
6
5
4
3
2
1
0
27Problem Build Cell Array of Roman Numerals
28Idea
- C1 I
- C2 II
- C3 III
-
- C2007 MMVII
-
- C3999 MMMXMXCIX
29A Conversion Problem
- 1904 11000 9100 010 41
- M CM
IV - MCMIV
301
9
0
4
MCMIV
311
9
0
4
M CM IV
321
9
0
4
M CM IV
M MM MMM
331
9
0
4
M CM IV
-
- C
- CC
- CCC
- CD
- D
- DC
- DCC
- DCCC
- CM
M MM MMM
341
9
0
4
M CM IV
-
- C
- CC
- CCC
- CD
- D
- DC
- DCC
- DCCC
- CM
X XX XXX XL L LX LXX LXXX XC
M MM MMM
351
9
0
4
M CM IV
-
- C
- CC
- CCC
- CD
- D
- DC
- DCC
- DCCC
- CM
X XX XXX XL L LX LXX LXXX XC
I II III IV V VI VII VIII IX
M MM MMM
Concatenate entries from these cell arrays
36Ones-Place Conversion
function r Ones2R(x) x is an integer that
satisfies 0 lt x lt 9 r is the Roman
numeral with value x. Ones 'I', 'II',
'III', 'IV', 'V', 'VI','VII', 'VIII',
'IX' if x0 r '' else r
Onesx end
37Tens-Place Conversion
function r Tens2R(x) x is an integer that
satisfies 0 lt x lt 9 r is the Roman
numeral with value 10x. Tens X', XX',
XXX', XL', L', LX',LXX', LXXX',
XC' if x0 r '' else r
Tensx end
38Hundreds-Place Conversion
function r Hund2R(x) d is an integer that
satisfies 0 lt x lt 9 r is the Roman
numeral with value 100x. Hund C', CC',
CCC', CD', D', DC',DCC', DCCC',
CM' if x0 r '' else r
Hundx end
39Thousands-Place Conversion
function r Thou2R(x) d is an integer that
satisfies 0 lt x lt3 r is the Roman
numeral with value 1000x. Thou M', MM',
MMM' if x0 r '' else r
Thoux end
40Back to Our Problem
- C1 I
- C2 II
- C3 III
-
- C2007 MMVII
-
- C3999 MMMXMXCIX
41Generate 1,,3999
0 lt a lt 3 0 lt b lt 9 0 lt c lt 9 0 lt d lt 9
42This Prints 0,,3999
- for a 03
- for b 09
- for c 09
- for d 09
- n a1000 b100 c10 d
-
- end
- end
- end
- end
43-
- n a1000 b100 c10 d
- if n gt 0
- Cn Thou(a) Hund(b)
- Tens(c) Ones(d)
- end
44Reverse Problem
- Given Roman Numeral, compute its value.
- Assume cell array C(3999,1) available
- C1 I
-
- C3999 MMMCMXCIX
45- function k RN2Int(r)
- r is a string that represents
- Roman numeral
- k is its value
-
- C RomanNum
- k1
- while strcmp(r,Ck)
- kk1
- end