16 Cell Arrays SetUp Subscripting Nested Loops String
16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations
Matrix vs. Cell Array Vectors and matrices store values of the same type in all components 3. 1 2 -1 9 A cell array is a special array whose individual components may contain different types of data -4 ‘c’ ‘o’ ‘m’ ‘ ’ ‘s’ ‘ 1’ ‘ 2’ ‘ ’ ‘M’ ‘a’ ‘t’ ‘ ’ ‘L’ ‘A’ ‘B’ ‘ ’ 1. 1 5 x 1 matrix ‘ ’ -1 4 x 5 matrix 5 ‘. ’ ‘c’ ‘o’ ‘m’ ‘ ’ 7 . 91 ‘M’ . 4 -1 7 3 × 2 cell array Insight Through Computing
Cell Arrays of Strings C= { ‘Alabama’, ’New York’, ’Utah’} C ‘Alabama’ ‘New York’ ‘Utah’ C= { ‘Alabama’; ’New York’; ’Utah’} C ‘Alabama’ ‘New York’ 1 -d cell array of strings ‘Utah’ Insight Through Computing Contrast with 2 -d array of characters M= [‘Alabama ’; . . . ‘New York’; . . . ‘Utah ’] M ‘A’ ‘l’ ‘a’ ‘b’ ‘a’ ‘m’ ‘a’ ‘ ‘ ‘N’ ‘e’ ‘w’ ‘ ‘ ‘Y’ ‘o’ ‘r’ ‘k’ ‘U’ ‘t’ ‘a’ ‘h’ ‘ ‘ ‘ ‘
Use braces { } for creating and addressing cell arrays Matrix n Create Cell Array n m= [ 5, 4 ; … 1, 2 ; … 0, 8 ] n Addressing m(2, 1)= pi Insight Through Computing Create C= { ones(2, 2), 4 ; … ‘abc’ , ones(3, 1) ; … 9 , ‘a cell’ } n Addressing C{2, 1}= ‘ABC’ C{3, 2}= pi disp(C{3, 2})
Example of a small cell array… C = { ‘Alabama’, ’New York’, ’Utah’}; C: ‘Alabama’ Insight Through Computing ‘New York’ ‘Utah’
Syntax Entries Separated by Commas C = { ‘Alabama’, ’New York’, ’Utah’}; Curly Brackets Insight Through Computing
Synonym C = { ‘Alabama’, ’New York’, ’Utah’}; C = cell(1, 3); C{1} = ‘Alabama’; C{2} = ‘New York’; C{3} = ‘Utah’; Application: Storing strings Insight Through Computing
“Vertical” cell array set-up C = { ‘Alabama’; ’New York’; ’Utah’}; C = cell(3, 1); C{1} = ‘Alabama’; C{2} = ‘New York’; C{3} = ‘Utah’; Application: Storing strings Insight Through Computing
A cell array to store other arrays… C= {‘Oct’, 30, ones(3, 2)}; is the same as C= cell(1, 3); % not necessary C{1}= ‘Oct’; C{2}= 30; C{3}= ones(3, 2); You can assign the empty cell array: Insight Through Computing D = {}
Example: Represent a deck of cards with a cell array D{1} = ‘A Hearts’; D{2} = ‘ 2 Hearts’; : D{13} = ‘K Hearts’; D{14} = ‘A Clubs’; : D{52} = ‘K Diamonds’; But we don’t want to have to type all combinations of suits and ranks in creating the deck… How to proceed? Insight Through Computing
Make use of a suit array and a rank array … suit = {‘Hearts’, ‘Clubs’, … ‘Spades’, ‘Diamonds’}; rank = {‘A’, ‘ 2’, ’ 3’, ’ 4’, ’ 5’, ’ 6’, … ’ 7’, ’ 8’, ’ 9’, ’ 10’, ’J’, ’Q’, ’K’}; Then concatenate to get a card. E. g. , str = [rank{3} ‘ ’ suit{2} ]; D{16} = str; So D{16} stores ‘ 3 Clubs’ Insight Through Computing
To get all combinations, use nested loops i = 1; % index of next card for k= 1: 4 % Set up the cards in suit k for j= 1: 13 D{i} = [ rank{j} ' ' suit{k} ]; i = i+1; end Insight Through Computing
Example: deal a 12 -card deck D: N: 1, 5, 9 4 k-3 E: 2, 6, 10 4 k-2 S: 3, 7, 11 4 k-1 W: 4, 8, 12 4 k Insight Through Computing
% Deal a 52 -card deck N = cell(1, 13); E = cell(1, 13); S = cell(1, 13); W = cell(1, 13); for k=1: 13 N{k} = D{4*k-3}; E{k} = D{4*k-2}; S{k} = D{4*k-1}; W{k} = D{4*k}; end Insight Through Computing
The “perfect shuffle” of a 12 -card deck A B C Insight Through Computing D E F G H I J K L
Step 1: Cut the deck A B C Insight Through Computing D E F G H I A B C D E F G H I J K L
Step 2: Alternate A B C D E F G H I A B C D E F 1 2 3 4 5 6 G H I J K L A G B H C I D J E K F L 1 2 3 4 5 6 7 8 9 10 11 12 Insight Through Computing
Step 2: Alternate A B C k D 1 G B 3 Insight Through Computing F G H I A B C D E F 1 2 3 4 5 6 G H I J K L 2 k-1 A E H C 5 I D 7 J E 9 J K L K F L 11
Step 2: Alternate A B C k D E F G H I A B C D E F 1 2 3 4 5 6 G H I J K L K F L 2 k A G B 2 Insight Through Computing H 4 C I 6 D J 8 E 10 12
Illustrate the shuffle with color % Set up a 52 -color spectrum C = cell(52, 1); for k=1: 52 f = (k-1)/51; C{k} = [f 0 1 -f]; end A 3 -vector for color Insight Through Computing Each cell in C has a different red-blue color representing one card
Using fill( , , C{k})… 8 7 6 5 4 3 2 1 0 After 8 shuffles you get back the original arrangement! Insight Through Computing
Example: Build a cell array of Roman numerals for 1 to 3999 C{1} = ‘I’ C{2} = ‘II’ C{3} = ‘III’ : C{2007} = ‘MMVII’ : C{3999} = ‘MMMXMXCIX’ Insight Through Computing
Example 1904 = 1*1000 + 9*100 + 0*10 + 4*1 = MCMIV Insight Through Computing CM IV
1 9 0 MCMIV Insight Through Computing 4
1 ‘M’ concat. ‘’ M MM MMM Concatenate entries from these cell arrays! Insight Through Computing 9 0 ‘CM’ concat. ‘’ C CC CD D DC DCCC CM 4 ‘’ ‘’ X XX XL L LX LXXX XC concat. ‘IV’ ‘’ I II IV V VI VIII IX
Ones-Place Conversion function r = Ones 2 R(x) % x is an integer that satisfies % 0 <= x <= 9 % r is the Roman numeral with value x. Ones = {'I', 'III', 'IV', . . . 'V', 'VII', 'VIII', 'IX'}; if x==0 r = ''; else r = Ones{x}; end Insight Through Computing
Ones-Place Conversion function r = Ones 2 R(x) % x is an integer that satisfies % 0 <= x <= 9 % r is the Roman numeral with value x. Ones = {'I', 'III', 'IV', . . . 'V', 'VII', 'VIII', 'IX'}; if x==0 r = ''; else r = Ones{x}; end Insight Through Computing
Similarly, we can implement these functions: function r = Tens 2 R(x) % x is an integer that satisfies % 0 <= x <= 9 % r is the Roman numeral with value 10*x. function r = Hund 2 R(x) % x is an integer that satisfies % 0 <= x <= 9 % r is the Roman numeral with value 100*x function r = Thou 2 R(x) % x is an integer that satisfies % 0 <= x <=3 % r is the Roman numeral with value 1000*x Insight Through Computing
We want all the Roman Numerals from 1 to 3999. We have the functions Ones 2 R, Tens 2 R, Hund 2 R, Thou 2 R. The code to generate all the Roman Numerals will include loops—nested loops. How many are needed? A: 2 Insight Through Computing B: 4 C: 6 D: 8
Now we can build the Roman numeral cell array for 1, …, 3999 for a = 0: 3 for b = 0: 9 for c = 0: 9 for d = 0: 9 n = a*1000 + b*100 + c*10 + d; if n>0 C{n} = [Thou 2 R(a) Hund 2 R(b). . . Tens 2 R(c) Ones 2 R(d)]; end end end Insight Through Computing
Now we can build the Roman numeral cell array for 1, …, 3999 for a = 0: 3 % possible values in thous place for b = 0: 9 % values in hundreds place for c = 0: 9 % values in tens place for d = 0: 9 % values in ones place n = a*1000 + b*100 + c*10 + d; if n>0 C{n} = [Thou 2 R(a) Hund 2 R(b). . . Tens 2 R(c) Ones 2 R(d)]; end Four strings concatenated together end The nth component of cell array C end Insight Through Computing
The reverse conversion problem Given a Roman Numeral, compute its value. Assume cell array C(3999, 1) available where: C{1} = ‘I’ C{2} = ‘II’ : C{3999} = ‘MMMCMXCIX’ Insight Through Computing
function k = RN 2 Int(r) % r is a string that represents % Roman numeral % k is its value C = Roman. Num; k = 1; while ~strcmp(r, C{k}) k=k+1; end Insight Through Computing
- Slides: 33