1 / 11

Data Structures

Data Structures. Chapter 8. Data Structures Overview. Data Structure : A variable that stores more than one value Matrices/vectors and character arrays are types of data structures MATLAB also provides two other types of data structures Cell Array Stores values of different types.

sine
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Chapter 8

  2. Data Structures Overview Data Structure: A variable that stores more than one value • Matrices/vectors and character arrays are types of data structures MATLAB also provides two other types of data structures • Cell Array • Stores values of different types. • Values can be strings, chars, doubles, matrices, etc… • Numerically Indexed(so you can loop through them) • Structure • Stores values of different types • Values can be can be strings, chars, doubles, matrices, etc… • Values are stored in “fields” (i.e. you can’t loop through them) • Useful for making databases

  3. MATLAB Matrix Rules Review Up until now we have mainly focused on what matrices and character arrays can do • Some General Rules… • Must have consistent #cols • Can’t have empty entries • Can’t mix variables of different type • i.e. you can’t mix doubles, ints, and chars

  4. Common Earth Science Data Sets Advanced National Seismic System (ANSS) • http://www.ncedc.org/anss/ • Near real-time data for all global earthquake events! • Not just numbers USGS Current Water Data • http://waterdata.usgs.gov/nwis/rt • Near real-time data for streams in all 50 states! • Not just numbers

  5. Cell Arrays: The Basics What if you wanted to make a data structure that could hold anything of any size? (assuming you have sufficient RAM) Matrices • Must have consistent dimensions • Must be all numeric (no chars/strings) Character Arrays (Strings) • Must have consistent dimensions (of characters) • Must contain only characters Cell Arrays • An array of cells • Each cell can hold anything • char, double, int, matrix, vector, string, logical • Each entry can be any size • Unlike matrices, a given cell can be empty • Technically, the cell doesn’t hold the value, it just contains a pointer to where the contents are stored. • This usually isn’t important for us as Earth scientists.

  6. Cell Arrays: Cell Indexing How do I create a cell array? • Cell arrays are created using curly braces { } • Follow the same indexing rules as matrices • If you use (), this is called cell indexing • Returns the cell(not the contents) of the specified index

  7. Cell Arrays: Content Indexing How do I create a cell array? • Cell arrays are created using curly braces { } • To get a cell’s contents use curly braces { } • Called content indexing • Returns the cell’s contents of the specified index • Typically, a string, some numeric value, or matrix

  8. Cell Arrays: Pre-Allocating How do I pre-allocate a cell array? • Use ‘cell(m,n)’ • Makes an empty m x n cell array • You can later add in the cell contents • Essentially the same as pre-allocating a matrix, except cells can be empty Why should I do this? • Cell arrays suffer from the same inefficiencies as matrices, so they should be pre-allocated whenever possible • This mainly applies to adding in cells in a loop

  9. One Practical Use of Cell Arrays: • Store strings of different lengths in one variable • Be careful about using ( ) and { } I pity the fool that doesn’t know the difference between cell indexing and content indexing!!

  10. Cell Arrays: Referring to Cell Contents • Referring to the contents of a cell may look intimidating • But it makes sense! • Because cell arrays are numerically indexed • Can use loops to go through entries

  11. Final Thoughts… Cell Arrays are a little confusing at first, but they are necessary so we can… • Mix strings of different lengths in a single variable • Mix numbers, matrices, vectors, strings, characters in a single variable Cell arrays are most commonly used in two situations: • You want to store a list of words that are different lengths • You want to read in a data file that has both numbers and words/letters • For this, we need to move onto “Advanced File I/O”

More Related