ARRAY REFERENCING 1 1. II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays.
4.3 Matrix Approach to Solving Linear Systems 1 Linear systems were solved using substitution and elimination in the two previous section. This section.
1 Copyright © 2015, 2011, 2007 Pearson Education, Inc. Chapter 4-1 Systems of Equations and Inequalities Chapter 4.
Maths for Computer Graphics
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Review of Matrix Algebra
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.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
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.
Matrix Definition A Matrix is an ordered set of numbers, variables or parameters. An example of a matrix can be represented by: The matrix is an ordered.
HAWKES LEARNING SYSTEMS math courseware specialists Copyright © 2011 Hawkes Learning Systems. All rights reserved. Hawkes Learning Systems College Algebra.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
MATRICES AND DETERMINANTS
Array Math. I. Definition MATLAB is known for its mathematical power! – It does vector and MATRIX operations automatically (most languages require you.
Matlab tutorial course Lesson 2: Arrays and data types
SYSTEMS OF EQUATIONS MATRIX SOLUTIONS TO LINEAR SYSTEMS
Euclidean m-Space & Linear Equations Row Reduction of Linear Systems.
IE 212: Computational Methods for Industrial Engineering
1 Arrays 2.Common Operations 1. Slicing 2. Diminution 3. Augmentation 1.
Logical Ops’ on Arrays When we need to compare arrays, find a number within an array, isolate all invalid numbers… 1. Logical Operators 2. Logical Operations.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
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.
Lesson 13-1: Matrices & Systems Objective: Students will: State the dimensions of a matrix Solve systems using matrices.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Traversing an array 1.Definition 2.Use the for loop 3.Some new functions 4.Use the while loop 1.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Matrix Arithmetic. A matrix M is an array of cell entries (m row,column ) and it must have rectangular dimensions (Rows x Columns). Example: 3x x.
13.1 Matrices and Their Sums
1 Chapter 3 Arrays (2) 1. Array Referencing 2. Common Operations 1. Slicing 2. Diminution 3. Augmentation 3. List of Commonly Used Built-in Functions 1.
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.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
Built-in Data Structures in Python An Introduction.
Lesson 11-1 Matrix Basics and Augmented Matrices Objective: To learn to solve systems of linear equation using matrices.
Class Opener:. Identifying Matrices Student Check:
Working with Arrays in MATLAB
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Notes 7.2 – Matrices I. Matrices A.) Def. – A rectangular array of numbers. An m x n matrix is a matrix consisting of m rows and n columns. The element.
Copyright © Cengage Learning. All rights reserved. 7 Linear Systems and Matrices.
Matrices and Systems of Linear Equations
Algebra II Honors Problem of the Day Homework page eoo The following system has been solved and there are infinite solutions in the form of (
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Multidimensional Arrays Computer and Programming.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Sections 2.4 and 2.5 Matrix Operations
12-1 Organizing Data Using Matrices
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
Matrix Operations SpringSemester 2017.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
Matlab tutorial course
All About Matrices.
2.2 Introduction to Matrices
Loop Statements & Vectorizing Code
Matlab Intro.
Loop Statements & Vectorizing Code
1.8 Matrices.
Matrix Operations SpringSemester 2017.
1.8 Matrices.
Working with Arrays in MATLAB
Matlab Intro.
Presentation transcript:

ARRAY REFERENCING 1 1

II. Array Referencing Assume an array has values. It is useful to “refer to” the elements contained within it – as smaller portions of the array or even individually. Because the values contained within the array may change when the program runs, the index (i.e. position) of the elements allows a mean of accessing them. MATLAB starts counting at 1. 2 ?… ?… 3 RD

II. Array Referencing How to refer to elements within a scalar? A vector? A matrix? A scalar has one single value – simply refer to the variable itself. age A vector has one dimension regardless whether it’s a row vector or a column vector. Use a single index to reference the values in a vector: ages(2) A matrix has two or more dimensions. Use an index for EACH dimension: FIRST: a row number, SECOND: a column number pressures(3,56) (More dimensions? Use another number for each additional dimension!) 3

Array Referencing - Vectors Vectors use a single value. Each value is called an “index”: x = [5; -1; 4]; %original vector sum = 0; %start sum at zero sum = sum + x(1); %add first element sum = sum + x(2); %add second element sum = sum + x(3); %add third element 4 Index This process of repeatedly adding numbers to a single variable is called a “running sum”

Array Referencing - Vectors Vectors use a single value. Each value is called an “index”: x = [5; -1; 4]; %original vector sum = 0; %start sum at zero sum = sum + x(1); %add first element sum = sum + x(2); %add second element sum = sum + x(3); %add third element Vectors have one dimension, so use a single index in parentheses to specify which element to use. Indexing starts at 1, and can go as high as how-many-elements-there-are. Yes, it seems quite repetitive… wouldn’t a loop make it easier? Hang in there… 5

Array Referencing - Matrices Matrices are similar. To access the 6 in this matrix: M = [1, 2, 3; 4, 5, 6; 7, 8, 9] Use : M(2,3) 6 Row number always first! Column number always second!

Array Referencing - Matrices Matrices are similar. To access the 6 in this matrix: M = [1, 2, 3; 4, 5, 6; 7, 8, 9] Use : M(2,3) It can be used directly: x = 7 * M(2,3); %Result? _____ 7 Row number always first! Column number always second! 42

Array Referencing - Matrices Matrices are similar. To access the 6 in this matrix: M = [1, 2, 3; 4, 5, 6; 7, 8, 9] Use : M(2,3) It can be used directly: x = 7 * M(2,3); %Result? _____ The row and column positions specified in the parentheses are referred to as “indices” (plural of “index”): 2 is the “row index” and 3 is the “column index”. 8 Row number always first! Column number always second! 42

Referencing To refer to “all” of a column or row, use the range operator by itself: V = M(:, 3); %from M, copy all rows in columns 3 to V 9 9 The same can be done with columns! V = M(2, :); % V gets a copy of all columns of row 2

ARRAY SLICING Accessing more than one element of an array 10

Array Slicing In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2, % columns 1 through 4 11 … ……

Array Slicing In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2, % columns 1 through 4 M1 = M(___,____); 12

Array Slicing In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2, % columns 1 through 4 M1 = M(1:2,____); 13

Array Slicing In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2, % columns 1 through 4 M1 = M(1:2,____); 14

Array Slicing In general, a slice is a "smaller piece" of something. The range operator is frequently used when getting a slice. % Copy all elements in rows 1 and 2, % columns 1 through 4 M1 = M(1:2,1:4); 15

Real-life #1: Eliminating bad data In wind tunnels, the data is obtained throughout the tunnel. However, data is usually flawed around the walls, or far away form the object itself. Given an array of pressure/temperature/or density obtained, only the ones far from the wall are kept for analysis! 16

Real-life #1: Eliminating bad data In wind tunnels, the data is obtained throughout the tunnel. However, data is usually flawed around the walls, or far away form the object itself. Given an array of pressure/temperature/or density obtained, only the ones far from the wall are kept for analysis! 17

ARRAY DIMINUTION Making arrays smaller Deleting an element, a row, a column, etc.. 18 Pronounce: “Dim’ – min – yoo’ – shun”

Array Diminution To eliminate the whole content, re-define it as an empty- vector: scores = []; %delete all scores To eliminate a single value from a vector, either take a slice: HighScores = [757, 65, -13, -89]; HighScores = HighScores(1:3); %deletes last score Or use the empty-vector: HighScores(4) = []; %removes 4 th score 19

Example Diminution After analyzing data, get rid of some data: in this case, assign the empty brackets [] For example, get rid of the number 8 in b below: 20 This action changes the original vector and cannot be undone.

Example Diminution After analyzing data, get rid of some data: in this case, assign the empty brackets [] For example, get rid of the number 8 in b below: 21 This action changes the original vector and cannot be undone.

Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 22 %”M

Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 23 %”M, all-rows

Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 24 %”M, all-rows, 1 st column

Array Diminution, cont. To eliminate an entire row/column: 1.Use the range operator, combined with 2.the empty-vector M = [1, 2, 3; 4, 5, 6]; M(:, 1) = [] … Read it as: 25 %”M, all-rows, 1 st column, delete!”

Array Diminution, cont. Question: Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] 26

Array Diminution, cont. Question: Can we eliminate a single value from a matrix? M = [1, 2, 3; 4, 5, 6]; M(2, 2) = [] 27 No – because that would mean some rows or columns would have more values than others.

AUGMENTING AN ARRAY Insert values at the end of an array (not in the middle, nor beginning) 28

Array Augmentation, review Augmentation = “Adding to” = making an array bigger. For example: V = [1, 2, 3]; To augment more columns, it’s much like doing a running total or running product: to the current variable, perform an action: V = [V, 4, 5, 6]; 29 Result: [ ___________________ ] ? 29 1, 2, 3, 4, 5, 6

Array Augmentation, review Augmentation = “Adding to” = making an array bigger. For example: V = [1, 2, 3]; To augment more columns, it’s much like doing a running total or running product: to the current variable, perform an action: V = [V, 4, 5, 6]; To augment with another row vector variable: V1 = [3, 4, 5]; V2 = [6, 7, 8]; V1 = [V1; V2]; 30 Makes a matrix! Result: __ __ __. 30 1, 2, 3, 4, 5, 6 Result: [ ___________________ ] ? 3, 4, 5 6, 7, 8

Array Augmentation, review Augmentation = “Adding to” = making an array bigger. For example: V = [1, 2, 3]; To augment more columns, it’s much like doing a running total or running product: to the current variable, perform an action: V = [V, 4, 5, 6]; To augment with another row vector variable: V1 = [3, 4, 5]; V2 = [6, 7, 8]; V1 = [V1; V2]; To augment with a column vector variable: V1 = [6; 8; 9]; V2 = [10; 20; 30]; V1 = [V1, V2]; 31 Makes a matrix! Why use a comma? ________________ Result: __ __ __. Result: __ __. __ 31 Result: [ ___________________ ] ? 1, 2, 3, 4, 5, 6 3, 4, 5 6, 7,

Array Augmentation, review Works for matrices, too: M1 = [1, 2, 3; 4, 5, 6]; %original matrix M1 = [M1; 7, 8, 9]; % attach a row to M1 M1 = [M1, [11, 2, 33; 44, 33, 22; 1, 0, 2]] M1 = Be sure to augment with the correct number of rows / columns! 32

Extending an array 33 Matrix b does not have 4 columns… mmm… what will it do?

Extending an array 34 Fills in with zeros.

Key Ideas I.Hardcoding arrays [], ; : ‘ Prefer these symbols when arrays are small, NOT when arrays are big. Exception: the colon can technically create big arrays instantly. II.Referencing Index = position number Use one index in vectors vector(index number) Use two indices in matrices matrix(row, colum) III.Common operations Slicing: concentrating on a piece of an array Diminution: getting rid of elements. Use =[ ]; Augmenting: “adding values” – increasing the size of an existing array Combine any symbols/method, as long as the array created is rectangular! IV.Common functions sum() prod() mean() max() min() Different results when applied to vector compared to matrices Go ahead, use MATLAB and arrays to check your math homework! 35

Helpful functions How many elements are in the array? 36 FunctionReturn value length(vector) Number of elements in the vector length(matrix) Highest dimension size(matrix,1) Number of rows in the matrix size(matrix,2) Number of columns in the matrix size(matrix) 1 by 2 array of row and column dimensions numel(array) Number of elements total in the array