Other Kinds of Arrays Chapter 11

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

1 Chapter 2 The Digital World. 2 Digital Data Representation.
Introduction to C Programming
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
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.
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
IT253: Computer Organization
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
CPS120: Introduction to Computer Science
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © – Curt Hill Types What they do.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
MATLAB ® for Engineers, Holly Moore Fourth Edition, Global Edition © Pearson Education Limited 2015 All rights reserved. Figure 11.1 MATLAB ® supports.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
EET 2259 Unit 13 Strings and File I/O
ECE 1304 Introduction to Electrical and Computer Engineering
The Machine Model Memory
How Computers Store Variables
Chapter 2: Introduction to C++
Chapter 4 C Program Control Part I
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture: MATLAB Chapter 1 Introduction
ITEC113 Algorithms and Programming Techniques
EPSII 59:006 Spring 2004.
Other Kinds of Arrays Chapter 11
MATLAB: Structures and File I/O
Chapter 2 Bits, Data Types & Operations Integer Representation
Introduction to the C Language
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Programming
Number and String Operations
Use of Mathematics using Technology (Maltlab)
Introduction to Java, and DrJava part 1
Building Java Programs
Introduction to Primitive Data types
Coding Concepts (Data- Types)
Data Types and Data Structures
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Introduction to Java, and DrJava
Chapter 2: Introduction to C++.
EECE.2160 ECE Application Programming
Primitive Types and Expressions
Unit 3: Variables in Java
EET 2259 Unit 13 Strings and File I/O
Introduction to Java, and DrJava part 1
EECE.2160 ECE Application Programming
Introduction to Primitive Data types
Presentation transcript:

Other Kinds of Arrays Chapter 11

Section 11.1 Data Types The primary data type (also called a class) in MATLAB is the array or matrix Within the array, MATLAB supports a number of different secondary data types (classes) The default is a double precision floating point number

MATLAB’s arrays can store different types of data Kinds of Data Stored in MATLAB Matrices Numeric Character Logical Symbolic Objects - Symbolic Toolbox Integer Floating Point multiple signed integer types multiple unsigned integer types single precision double precision complex real

The difference between array types and data types may be confusing Consider the following analogy

Numeric Data Types Numeric data is stored in numeric arrays The default data type is double precision floating point Every time you enter a number into MATLAB, the program assumes you’ve entered a double MATLAB conforms to the IEEE standards that specify the meaning of the double data type

When you define numeric values they default to doubles Each value in a double array needs 8 bytes of memory to store

There are 6 values in the C array – therefore it requires 6 x 8 = 48 bytes of memory

Value limitations The biggest number you can store in a double can be found using the realmax function The smallest number you can store in a double can be found using the realmin function

Single Precision floating point numbers This data type is a new feature in MATLAB 7 Uses half the storage space of a double Each value requires 4 bytes = 32 bits

The grid symbol indicates a numeric array – double, single or integer It’s necessary to use the single function to change the value of 5 (which is a double by default) into a single

Engineers will rarely need to convert to single precision numbers, because today’s computers have plenty of storage space for most applications, and will execute most of the problems we pose in extremely short amounts of time

When would you use the short data type instead of double values? In some numerical analysis applications you may be able to improve the run time of a long problem by changing from double to single precision However, round off error becomes more of a problem.

Consider the harmonic series Shorthand for the harmonic series This series diverges -it just keeps getting bigger the more terms you add

For large numbers of steps the results are different using double and single data types

Why? When the series gets big enough the value of 1/n is so small that the computer can’t distinguish it from 0 This occurs at the value of realmin Since doubles can differentiate between smaller numbers than singles the summation is valid for more steps

Complex numbers Default is double Twice as much storage is needed because the real and imaginary components must be stored Could also be stored as a single or integer

Character and String Data Character arrays store character information A character array is produced from a string

Each character is a separate element in the array

The fifth element of the H array is the letter y

Any string represents a character array in MATLAB Each character requires 2 bytes of storage

The ‘ab’ symbol indicates a character array Spaces are characters too

How are characters stored in MATLAB? All information in computers is stored using a series of zeros and ones ASCII – Used in small computers EBCDIC – Used in mainframes and super computers You can think of this list of 0’s and 1’s as a base two number

Comparison between base 2 and base 10 Base 2 ”binary” Base 10 ”decimal” 1 10 2 11 3 100 4 101 5 110 6 111 7 1000 8

Every character stored using ASCII or EBCDIC code has both a binary representation and a decimal equivalent When we ask MATLAB to change a character to a double, the number we get is the decimal equivalent in the ASCII coding system

MATLAB includes functions to change data types Use the double function to convert to a double precision floating point number Use char to convert a number to a character

You shouldn’t mix data types in calculations or in arrays Notice that the ans array has two characters If you attempt to create an array with both character and numeric data, the array defaults to all characters There is no character equivalent to 3, so MATLAB adds a space to the array

If you try to perform arithmetic with a combination of character and numeric data, MATLAB converts the character to its decimal equivalent Remember that a has a decimal equivalent of 97

Symbolic Data Covered in more detail in a separate chapter The symbolic toolbox uses symbolic data to perform symbolic algebraic calculations Create a symbolic variable using the sym function

Storage requirements for symbolics vary, depending on how big the expression is.

The cube symbol indicates a symbolic array Symbolic variables can be grouped into arrays, just like other data types

Logical Data Types Logical data can have only one of two values True False MATLAB uses 0 to represent false and 1 to represent true

The check mark indicates a logical array Although a logical array contains the information true and false, MATLAB represents it as 0 and 1 We don’t usually create logical arrays by entering true and false values. Usually they are the result of logical operations

Notice that x and y are numeric arrays and z is a logical array We can interpret this result to mean that x>y is false for elements 1 and 4, and true for elements 2,3 and 5 These arrays are used in logical functions, and are not usually even seen by the user.

In this example the find command used the result of the comparison x>y to determine that elements 2, 3, and 5 met the criteria

Sparse Arrays Both double precision arrays and logical arrays can be stored in either full matrices, or as sparse matrices. Sparse matrices are “sparsely populated”, meaning many or most of the values in the array are zero Identity matrices are examples of sparse matrices

Sparse matrices require less space than the corresponding numeric or logical matrices If we store sparse arrays in the full matrix format, it takes 8 bytes of storage for every data value, whether they are zeros or not The sparse matrix format only stores the non-zero values, and remembers where they are this strategy saves a lot of space

Compare the size of N and P (8,000,000 vs. 24,008) N is a 1000x1000 identity matrix P is the same matrix, stored using the sparse strategy

Section 11.2 Multidimensional Arrays Sometimes you may want to store data in multidimensional arrays Rows Columns Pages Additional dimensions are possible

Multidimensional arrays are grouped into pages rows columns pages

Page 1 Page 2 Page 3 Page 4 Imagine that you would like to store each of these 4 two-dimensional arrays into 1 three-dimensional array with four pages.

You must define each page separately You must define each page separately. Read the first definition statement as “all the rows, all the columns, in page 1”

Section 11.3 Creating Character Arrays We can create two dimensional character arrays only if the number of elements in each row is the same

This statement doesn’t work, because the number of characters in each line is different

The char functions “pads” the array with blanks The char functions “pads” the array with blanks. Notice that the array size is 6 rows by 7 columns Character arrays can store any of the characters defined in the ASCII coding scheme – including the symbols for numbers

Let’s combine an array of test scores and names 98 84 73 88 95 100 Names Holly Steven Meagan David Michael Heidi Numeric information Character information

Notice that table is a character array When we tried to store the two different data types into the same array, MATLAB interpreted the numbers as the ASCII equivalent of characters

num2str In order to store the two different data types in the same array, we’ll need to convert the numbers into the corresponding characters num2str

The num2str function converted the array of scores into the corresponding characters

Creation of file names A useful application of character arrays and the num2str function is the creation of file names There are occasions when you may want to save data into .dat or .mat files, but you don’t know ahead of time how many files will be required my_data1.dat my_data2.dat my_data3.dat Etc.

For example…. Load a file of unknown size called some_data Create a number of new files, one for each column from the input file

Input and Output Output files Data from input file Data in File1 Data in File2 1 2 3 4 5 6 7 8 9 10 11 12 1 4 7 10 2 5 8 11 Data in File3 3 6 9 12

Load the input file Determine the number of rows and columns Create the file names Extract the data Save the data into the new files

Section 11.4 Cell Arrays The cell array can store different types of data inside the same array Each element in the array is also an array

These three arrays all store different data types

Create a cell array using curly braces

To access a particular element inside an array stored in a cell array, you must use a combination of curly braces and parentheses