Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.

Slides:



Advertisements
Similar presentations
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Advertisements

CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Spring R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Final Exam ReviewFinal Exam Review –Assessment –Exam Format –C –Matlab.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Upcoming Exam – April 8Upcoming Exam – April 8 Structures ReviewStructures.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Cells and Structures Array Cells and Array Structures.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Solving Linear Equations UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Matlab tutorial course Lesson 2: Arrays and data types
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Regression UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
March R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Grades & Lab 12Grades & Lab 12 Upcoming ExamUpcoming Exam StructuresStructures.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
Class Info E177 January 22, me. berkeley
Regular Expressions: Searching strings for patterns April 24, 2008 Copyright , Andy Packard and Trent Russi. This work is licensed under the Creative.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
E177, Reference/Assignment for Classes: Intro February 12, Copyright , Andy Packard. This work.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Structures (Chapter 7, pg ) Data types encountered so far – numeric arrays, called double arrays – character arrays, called char arrays – container arrays, called cell arrays Today we cover structure arrays, called struct arrays. Like cell arrays, structure arrays allow you to group different data types together. Example: create a 1-by-1 structure with 3 fields >> student.Name = ’Fred Smith’; >> student.SID = ; >> student.Scores = [ ]; The variable student is created. It is a structure containing 3 fields.

class, size, fieldnames, isfield Recall example >> student.Name = ’Fred Smith’; >> student.SID = ; >> student.Scores = [ ]; Student is a 1-by-1 struct, with 3 fields, whose names are ’Name’ and ’SID’ and ’Scores’ >> size(student) >> class(student) >> fieldnames(student) >> isfield(student,’Name’) >> isfield(student,’Names’)

Accessing fields Access the contents of the fields simply by typing VariableName.FieldName So, in this case, we can do >> student.Name >> student.SID >> student.Scores student.Scores is 1-by-4 double array. Everything works. For example, access its 1 st element. >> student.Scores(1) Or, access its last element. >> student.Scores(end)

Accessing fields (continued) Can also assign as you would expect. Change the 3 rd score to 99. >> student.Scores(3) = 99;

Structure Arrays How about using a struct to keep track of E77 students? But there are 300+ of you. We need a struct array –with 300 elements –each with fields ’Name’, ’SID’ and ’Scores’. So far, every object in Matlab can be an array (not just scalar). Same holds for struct Can construct this by just declaring the the 2 nd entry, and then the 3 rd entry, and so on…

Growing the struct Add the 2 nd student. Just assign, using the index… >> student(2).Name = ’William Hung’; >> student(2).SID = ; >> student(2).Scores = [ ]; Analogy: This works for all Matlab arrays. You can “grow” them simply by assigning beyond their current size. >> A = 1.3; >> A(2) %error, A only has 1 element >> A(2) = 4.7 % A is now 1-by-2 >> A(5) = 26 %A is now 1-by-5

Growing the struct (continued) Add the 3 rd student. Just assign, using the index… >> student(3).Name = ’N. Coughlin’; >> student(3).SID = ; >> student(3).Scores = [ ]; Check that student is a struct array >> size(student) >> student >> whos

Accessing a field of Struct Array Recall that student is 1-by-3 struct array. One of its fields is named ’Scores’. In each case below, the expression is a 1-by-4 double array student(1).Scores student(2).Scores student(3).Scores What is student.Scores by itself?

Comma-separated Lists By itself, student.Scores is literally interpreted as Student(1).Scores, Student(2).Scores, Student(3).Scores 1-by-4 array comma Explicitly, based on the data, it is literally equal to [ ], [ ], [ ] So, a field reference of a struct array represents a comma- separated list of the elements. A comma-separated list is not an object class (like double, char, cell or struct), but it can be used effectively in expressions…

Student.Scores vs [Student.Scores] Student.Scores actually represents [ ], [ ], [ ] which is 3 arrays (all happen to be 1-by-4) separated by commas Recall that [[ ], [ ], [ ]] is a 1-by-12 array (note the extra square brackets). Therefore, [student.Scores] is simply the 1-by-12 array [ ]

Application of RESHAPE [Student.Scores] is a 1-by-12 array of test scores, [s1t1 s1t2 s1t3 s1t4 s2t1 s2t2 s2t3 s2t4 s3t1 s3t2 s3t3 s3t4] –The first 4 elements are the test scores of student 1 –The next 4 elements are the test scores of student 2 –The next 4 elements are the test scores of student 3 It would be nice to have a 3-by-4 or 4-by-3 array of the scores. The 3-by-4 case is “hard”, since the order in memory is different. The 4-by-3 case is “easier” since the order in memory is the same. Tests Students Tests Students

RESHAPE will work Try reshape([Student.Scores],[4 3]) To get the 3-by-4 case, you simply need to transpose the 4-by-3 case, so its not much harder (but Matlab shuffles everything in memory) reshape([Student.Scores],[4 3])’

Multiple {} Reference of Cell Array If M is a cell array (say 6-by-7), then M([2 4],[7 5]) is a 2-by-2 cell array, drawn from M. However, M{[2 4],[7 5]} asking for the contents of 4 cells. The interpretation of this is again a comma-separated list. Therefore, M{[2 4],[7 5]} Is literally M{2,7}, M{4,7}, M{2,5}, M{4,5}