lecture9a - Introduction to Computer Programming with...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming with MATLAB CEE/MAE M20 Lecture 9a Data Abstraction CEE/MAE M20 Reading 2 CEE/MAE M20 Thus far, we have learned how to store data as Scalar variables 1-D and 2-D arrays This is rather limiting, so we’d like to enhance this. Easier to see why with a few examples . 3 CEE/MAE M20 Suppose we wish to store a list of the names of states . A single state’s name would be stored as an array: >> a = ‘ New York ’; >> a(1) ans = N >> a(2) ans = e 4
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CEE/MAE M20 Suppose we wish to store a list of the names of states . Multiple states would be stored in a 2-D array. But every row has to be the same length! >> M = [‘ New York ’; ‘ Utah ’]; Error using vertcat Dimensions of matrices being concatenated are not consistent. >> M = [‘ New York ’; ‘ Utah ’]; >> M(2,1:4) ans = Utah >> M(2,5:8) ans = 5 Really annoying to put extra space in! CEE/MAE M20 Same row-column syntax as for arrays This motivates the use of more flexible data storage A cell array is a special type of array that can store data of different type and size. Example: A 3 x 2 cell array 6 -4 -1 ‘.’ ‘ ’ ‘ ’ ‘c’ ‘o’ ‘m’ 0.4 -1 7 5 0.91 ‘M’ 7 To create this: >> C = { [-4 -1], [‘. ’; ‘com’]; ... [5; 0.91], 7; ... ‘M’, [0.4 -1 7]}; Enclose cell array with { }, instead of [ ] To access elements: >> C{2,1} ans = 5.0000 0.9100 CEE/MAE M20 This motivates the use of more flexible data storage A cell array is a special type of array that can store data of different type and size. Example: A 3 x 2 cell array 7 -4 -1 ‘.’ ‘ ’ ‘ ’ ‘c’ ‘o’ ‘m’ 0.4 -1 7 5 0.91 ‘M’ 7 To access elements: >> C{2,1} ans = 5.0000 0.9100 CEE/MAE M20 This motivates the use of more flexible data storage A cell array is a special type of array that can store data of different type and size. Example: A 3 x 2 cell array 8 -4 -1 ‘.’ ‘ ’ ‘ ’ ‘c’ ‘o’ ‘m’ 0.4 -1 7 5 0.91 ‘M’ 7 To access elements: >> C{3,2} ans = 0.4000 -1.0000 7.0000
Image of page 2
CEE/MAE M20 This motivates the use of more flexible data storage A cell array is a special type of array that can store data of different type and size. Example: A 3 x 2 cell array 9 -4 -1 ‘.’ ‘ ’ ‘ ’ ‘c’ ‘o’ ‘m’ 0.4 -1 7 5 0.91 ‘M’ 7 To access elements of elements: >> C{3,2}(2) ans = -1 CEE/MAE M20 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 5 matrix 5 x 1 matrix Insight Through Computing CEE/MAE M20 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 CEE/MAE M20 Back to our example of state names 12 >> C = {‘New York’,’Utah’}; ‘New York’ ‘Utah’ C: >> C{1} ans = New York
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern