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

Slides:



Advertisements
Similar presentations
Roman Numerals.
Advertisements

L16. Cell Arrays Set-Up Subscripting Nested Loops String Manipulations.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7.9Arrays of Pointers Arrays can contain pointers For.
Reagan/Clinton Compare/Contrast Essay. I. Reagan and Clinton both argue... but... A.Reagan argues B.Clinton argues.
Boost Spirit V2 A cookbook style guide to parsing and output generation in C++ Joel de Guzman Hartmut Kaiser
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Arrays  An array is a collection of like elements.  There are many engineering applications that use arrays.  MATLAB ® stores data in arrays and performs.
1 Ancient Numeration Systems Thank you Dr. Wyatt.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to Exploratory Descriptive Data Analysis in S-Plus Jagdish S. Gangolly State University of New York at Albany.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Lecture 7 C Pointers Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Chapter 8 Arrays and Strings

Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
 Pearson Education, Inc. All rights reserved Arrays.
Roman Numerals. Roman Numerals - Past and Present  Romans used them for trading and commerce.  When Romans learned to write they needed a way to write.
Roman Numerals. The Numbers I-1 II-2 III-3 IV-4 V-5 VI-6 VII-7 VIII-8 IX-9 X-10 C-100 D-500 M-1000.
By.. Miss Prangtip Kaewsuphot
Matlab tutorial course Lesson 2: Arrays and data types
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
IT-101 Section 001 Lecture #3 Introduction to Information Technology.
Roman Numerals Learning objective: To learn how to count using Roman numerals.
1 EPSII 59:006 Spring Exam Dates 3 Homework Grading In general, programs that do not compile and run will be given no more than 25% credit Points.
Chapter 8 Arrays and Strings
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 8: Arrays.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
JavaScript: The First Parts Part Three Douglas Crockford Yahoo! Inc.
Medical Math Health Occupations Education Mrs. Rose.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Matlab Basic. MATLAB Product Family 2 3 Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit.
¸üµÖŸÖ ×¿ÖÖÖ ÃÖÓãÖÖ,ÃÖÖ ŸÖÖ¸üÖ ú´ÖÔ¾Ö߸ü ×¾ÖªÖ¯ÖϲÖÖê× ¬Ö­Öß, ´Ö¬µÖ ×¾Ö³Ö֐Ö, ÃÖÖŸÖÖ¸üÖ ¿ÖÖ»ÖêµÖ †³µÖÖÃ֍Îú´ÖÖ­ÖãÃÖÖ¸ ÃÖÓãÖêŸÖᯙ ÃÖê¾ÖúÖÓ­Öß ŸÖµÖÖ¸ü.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Introduction to Exploratory Descriptive Data Analysis in S-Plus Jagdish S. Gangolly State University of New York at Albany.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
1 Notation and Specification of Concurrency n Concurrency Topics  1. Sequential programming notation  2. Expressing concurrency with co and process 
StructureStructure. Outline Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions.
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Introduction to the new Mathematics Primary Curriculum Spring 2014.
Learning Objective  Place value(Indian and International system of numeration)  Comparison of numbers( Ascending and Descending Order)  Roman numbers.
Maths in Year 3 at St Anselm’s.
Compare/Contrast English Bill of Rights vs U. S. Bill of Rights
Structure, Unions, typedef and enumeration
Parents Guide to Calculation
FP1 Matrices Introduction
Roman Numerals Adding value to letters I, V, X, L, C, D, M 11/30/2018.
The Coordinate Plane -5 5.
Roman Numerals.
Write these notes in your spirals!
Roman Numerals Used for some drugs and solutions, and used at times while ordering supplies Key symbols: I, V, X, L, C, D, M Arabic Roman.
Roman Numerals.
Roman Numerals.
Roman Numerals.
January.
Roman Numerals.
Roman Numerals.
Presentation transcript:

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

Insight Through Computing Matrix vs. Cell Array ‘c’‘o’‘m’‘ ’‘s’ ‘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’ ‘ ’ ‘.’‘.’ ‘m’‘c’‘o’ 3 × 2 cell array 4 x 5 matrix 5 x 1 matrix

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

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}= pi disp(C{3,2})

Insight Through Computing Example of a small cell array… C = { ‘Alabama’,’New York’,’Utah’}; C: ‘Alabama’ ‘New York’ ‘Utah’

Insight Through Computing Syntax C = { ‘Alabama’,’New York’,’Utah’}; Curly Brackets Entries Separated by Commas

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: D = {}

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?

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 …

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 To get all combinations, use nested loops

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

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 AB L CDEFGH IJ K The “perfect shuffle” of a 12-card deck

Insight Through Computing AB L CDEFGH IJ K ABCDEF L GH IJ K Step 1: Cut the deck

Insight Through Computing Step 2: Alternate AB L CDEFGH IJ K ABCDEF L GH IJ K AG L BHCIDJ EK F

Insight Through Computing Step 2: Alternate AB L CDEFGH IJ K ABCDEF L GH IJ K AG L BHCIDJ EK F k 2k-1

Insight Through Computing Step 2: Alternate AB L CDEFGH IJ K ABCDEF L GH IJ K AG L BHCIDJ EK F k 2k

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

Insight Through Computing Using fill(,, C{k})… After 8 shuffles you get back the original arrangement!

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 numerals for 1 to 3999

Insight Through Computing Example 1904 = 1* * *10 + 4*1 = M CM IV

Insight Through Computing 1904 MCMIV

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 1904 Concatenate entries from these cell arrays! ‘M’ concat. ‘CM’ concat. ‘’ concat. ‘IV’

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

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

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

Insight Through Computing We want all the Roman Numerals from 1 to 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: 2B: 4C: 6D: 8

Insight Through Computing for a = 0:3 for b = 0:9 for c = 0:9 for d = 0:9 n = a* b*100 + c*10 + d; if n>0 C{n} = [Thou2R(a) Hund2R(b)... Tens2R(c) Ones2R(d)]; end Now we can build the Roman numeral cell array for 1,…,3999

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* b*100 + c*10 + d; if n>0 C{n} = [Thou2R(a) Hund2R(b)... Tens2R(c) Ones2R(d)]; end Now we can build the Roman numeral cell array for 1,…,3999 Four strings concatenated together The n th component of cell array C

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

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