33
Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

  • View
    223

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

16. Cell Arrays

Set-UpSubscripting Nested LoopsString Manipulations

Page 2: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Matrix vs. Cell Array

3.1

2

-1

9

1.1

‘c’ ‘o’ ‘m’ ‘ ’ ‘s’

‘1’ ‘1’ ‘1’ ‘2’ ‘ ’

‘ ’ ‘ ’ ‘L’ ‘A’ ‘B’

‘M’ ‘a’ ‘t’ ‘ ’ ‘ ’

Vectors and matrices store values of the same type in all components

A cell array is a special array whose individual components may contain different types of data

.4‘M’ -1 7

.91

5

-4 -1

7

‘ ’‘.’ ‘ ’

‘m’‘c’ ‘o’

3 × 2 cell array

4 x 5matrix5 x 1

matrix

Page 3: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Cell Arrays of Strings

C= { ‘Alabama’,’New York’,’Utah’}

C ‘Alabama’ ‘New York’ ‘Utah’

C= { ‘Alabama’;’New York’;’Utah’}

‘Alabama’

‘New York’

‘Utah’

C

1-d cell array of strings

M= [‘Alabama ’; ... ‘New York’; ... ‘Utah ’]

‘A’ ‘l’ ‘a’ ‘b’ ‘a’

‘N’ ‘e’ ‘w’ ‘ ‘ ‘Y’

‘U’ ‘t’ ‘h’

‘m’ ‘a’

‘o’ ‘r’ ‘k’

‘ ‘

‘ ‘‘a’ ‘ ‘‘ ‘‘ ‘

M

Contrast with 2-d array of characters

Page 4: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Use braces { } for creating and addressing cell arrays

Matrix

Create

m= [ 5, 4 ; … 1, 2 ; … 0, 8 ]

Addressing

m(2,1)= pi

Cell Array

Create

C= { ones(2,2), 4 ; … ‘abc’ , ones(3,1) ; … 9 , ‘a cell’ }

Addressing

C{2,1}= ‘ABC’C{3,2}= pidisp(C{3,2})

Page 5: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Example of a small cell array…

C = { ‘Alabama’,’New York’,’Utah’};

C: ‘Alabama’ ‘New York’ ‘Utah’

Page 6: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Syntax

C = { ‘Alabama’,’New York’,’Utah’};

Curly Brackets

Entries Separated by Commas

Page 7: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

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

Page 8: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

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

Page 9: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

A cell array to store other arrays…

C= {‘Oct’, 30, ones(3,2)};

is the same asC= cell(1,3); % not necessary

C{1}= ‘Oct’;

C{2}= 30;

C{3}= ones(3,2);

You can assign the empty cell array: D = {}

Page 10: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

D{1} = ‘A Hearts’;

D{2} = ‘2 Hearts’;

:

D{13} = ‘K Hearts’;

D{14} = ‘A Clubs’;

:

D{52} = ‘K Diamonds’;

Example: Represent a deck of cards with a cell array

But we don’t want to have to type all combinations of suits and ranks in creating the deck… How to proceed?

Page 11: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

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’

Make use of a suit array and a rank array …

Page 12: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

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

end

To get all combinations, use nested loops

Page 13: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

N: 1,5,9

E: 2,6,10

S: 3,7,11

W: 4,8,12

D:

4k-3

4k-2

4k-1

4k

Example: deal a 12-card deck

Page 14: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

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

Page 15: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

A B LC D E F G H I J K

The “perfect shuffle” of a 12-card deck

Page 16: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

A B LC D E F G H I J K

A B C D E F

LG H I J K

Step 1: Cut the deck

Page 17: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Step 2: Alternate

A B LC D E F G H I J K

A B C D E F

LG H I J K

A G LB H C I D J E K F1 2 3 4 5 6 7 8 9 10 11 12

1 2 3 4 5 6

Page 18: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Step 2: Alternate

A B LC D E F G H I J K

A B C D E F

LG H I J K

A G LB H C I D J E K F1 3 5 7 9 11

1 2 3 4 5 6

k

2k-1

Page 19: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Step 2: Alternate

A B LC D E F G H I J K

A B C D E F

LG H I J K

A G LB H C I D J E K F 2 4 6 8 10 12

1 2 3 4 5 6k

2k

Page 20: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

% 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

Illustrate the shuffle with color

Each cell in C has a different red-blue color representing

one card

Page 21: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

01

2

34

5

6

8

7

Using fill( , , C{k})…

After 8 shuffles you get back the original arrangement!

Page 22: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

C{1} = ‘I’

C{2} = ‘II’

C{3} = ‘III’

:

C{2007} = ‘MMVII’

:

C{3999} = ‘MMMXMXCIX’

Example: Build a cell array of Roman numeralsfor 1 to 3999

Page 23: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Example

1904 = 1*1000 + 9*100 + 0*10 + 4*1

= M CM IV

= MCMIV

Page 24: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

1 9 0 4

MCMIV

Page 25: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

‘’

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

1 9 0 4

Concatenateentries fromthese cell arrays!

‘M’ concat. ‘CM’ concat. ‘’ concat. ‘IV’

Page 26: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Ones-Place Conversion

function r = Ones2R(x) % x is an integer that satisfies % 0 <= x <= 9 % r is the Roman numeral with value x. Ones = {'I', 'II', 'III', 'IV', ... 'V', 'VI','VII', 'VIII', 'IX'}; if x==0 r = ''; else r = Ones{x}; end

Page 27: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Ones-Place Conversion

function r = Ones2R(x) % x is an integer that satisfies % 0 <= x <= 9 % r is the Roman numeral with value x. Ones = {'I', 'II', 'III', 'IV', ... 'V', 'VI','VII', 'VIII', 'IX'}; if x==0 r = ''; else r = Ones{x}; end

Page 28: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Similarly, we can implement these functions:

function r = Tens2R(x)

% x is an integer that satisfies

% 0 <= x <= 9

% r is the Roman numeral with value 10*x.

function r = Hund2R(x)

% x is an integer that satisfies

% 0 <= x <= 9

% r is the Roman numeral with value 100*x

function r = Thou2R(x)

% x is an integer that satisfies

% 0 <= x <=3

% r is the Roman numeral with value 1000*x

Page 29: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

We want all the Roman Numerals from 1 to 3999. We have the functions Ones2R, Tens2R, Hund2R, Thou2R.

The code to generate all the Roman Numerals will include loops—nested loops. How many are needed?

A: 2 B: 4 C: 6 D: 8

Page 30: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

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} = [Thou2R(a) Hund2R(b)... Tens2R(c) Ones2R(d)]; end end end endend

Now we can build the Roman numeral cell array for 1,…,3999

Page 31: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

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} = [Thou2R(a) Hund2R(b)... Tens2R(c) Ones2R(d)]; end end end endend

Now we can build the Roman numeral cell array for 1,…,3999

Four strings concatenated together

The nth component of cell array C

Page 32: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

Given a Roman Numeral, compute its value.

Assume cell array C(3999,1) available where:

C{1} = ‘I’ C{2} = ‘II’ : C{3999} = ‘MMMCMXCIX’

The reverse conversion problem

Page 33: Insight Through Computing 16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations

Insight Through Computing

function k = RN2Int(r)% r is a string that represents % Roman numeral% k is its value C = RomanNum;k = 1;while ~strcmp(r,C{k}) k=k+1;end