Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane.
2D Plots 1 ENGR 1181 MATLAB 12.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Solving systems using matrices
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.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Wednesday, July 15, 2015 EQ: What are the similarities and differences between matrices and real numbers ? Warm Up Evaluate each expression for a = -5,
Matrix Mathematics in MATLAB and Excel
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Chapter 10 Review: Matrix Algebra
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
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.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
For Loops 1 ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
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.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
4.3 Matrix Multiplication 1.Multiplying a Matrix by a Scalar 2.Multiplying Matrices.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
The goal is to give an introduction to the mathematical operations with matrices. A matrix is a 2-dimensional arrangement of (real valued) data. The data.
2D Plots 2 ENGR 1181 MATLAB 13. Plotting in the Real World 2D plots generated by MATLAB can be used in a variety of fields, including the one shown here,
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Array Operations ENGR 1181 MATLAB 4.
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.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Sec 4.1 Matrices.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4.1 Exploring Data: Matrix Operations ©2001 by R. Villar All Rights Reserved.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
13.4 Product of Two Matrices
12-1 Organizing Data Using Matrices
Matrices Rules & Operations.
Linear Algebra review (optional)
Chapter 7 Matrix Mathematics
Matrix Operations.
Matrix Operations.
Matrix Operations SpringSemester 2017.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Vectorized Code, Logical Indexing
CSCI N207 Data Analysis Using Spreadsheet
Array Creation ENGR 1181 MATLAB 02.
Matlab Intro.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Linear Algebra review (optional)
1.8 Matrices.
Matrix Operations SpringSemester 2017.
1.8 Matrices.
Working with Arrays in MATLAB
Matlab Intro.
Announcements.
Presentation transcript:

Array Accessing and Strings ENGR 1187 MATLAB 3

Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

What is Addressing (indexing)?  Each element in a vector has an address, also called an index  MATLAB indexing starts at 1 (not at 0!)  We can access/retrieve/extract the individual elements by referring to their addresses  Useful for transforming data or doing calculations with only part of a vector

Recall from the previously class that seismic data is important in structural design for civil engineers. Accessing data from an array at a certain location in California allows engineers to design their structures according to vibrational data in a specific region. This allows the building to be designed to this standard but not overdesigned to more extreme data in other regions. Array Accessing In The Real World

Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

Vector Addressing Example  Define a vector with 9 elements: >> v = [ ]; We can access the elements individually: >> v(4) ans = 21

Vector Addressing Example We can retrieve any element by indexing: >> v(7) ans = 30 >> v(9) ans = 36 We can assign individual vector elements to variables: >> B= v(7) B = 30 >> C=v(9) C = 36

Vector Addressing Examples We can add elements together. Recall: B = v(7), C = v(9) >> D= B + C D = 66 We can also add elements directly: >> v(4) + v(7) ans = 51

Changing Element Values  We can change an element in a vector by directly assigning a new value to a specific address.  Let’s change the 6 th element of v to 90: v= [ ] >> v(6) = 90; >> v v =

Addressing Column Vectors Addressing (indexing) an element in a column vector works the same way as with a row vector: >> col = [25; 30; 35; 40; 45; 50] >> t = col(4) t = 40

Vector Functions  MATLAB has MANY built-in functions we can use with vectors max() min() sum() length() …etc.

Vector Functions Examples length() gives us the number of elements in a vector >> fun = [ ]; >> length(fun) ans = 5

Vector Functions Examples Zeros() gives us a vector or matrix of zeros >> nothing = zeros (1, 7) nothing =

Vector Functions Examples ones() gives us a vector/matrix of all ones >> single = ones(1, 12) single =

Addressing a Range of Elements  The colon operator allows us to access a range of elements in a vector  This is useful if we want to extract or alter only a portion of an existing vector

Example: Addressing a Range Define a vector: >> vec = [ ]; Select elements 3 through 7 in 'vec': >> vec(3:7) vec =

Example: Addressing a Range We can access a range of elements in any vector and assign them to a new variable. Recall that vec = [ ] >> t= vec(2:5) t =

Vector Modifications We can add elements to any existing vector. Recall that 'vec' has 8 elements: vec = [ ] >> vec(9: 12)= [ ] vec =

Vector Modifications We can create new vectors made up of elements from previously defined vectors: >> E = [ ]; >> G = [ ]; >> K = [ E(1:3) G(3:4)] K =

Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)

Matrix Addressing  Matrix addressing works very similarly to vector addressing  Individual elements are addressed by their row number and column number: (m, n)

Matrix Addressing Example Let's define a matrix, then access some elements: >> data = [ ; ] data = >> data (2,3) ans = 8

Matrix Addressing Example We can perform mathematical operation with matrix elements. Let's add two values from our matrix called 'data': data = >> data_sum= data(1,2) + data(2,4) data_sum = 12

Colon Operator With Matrices  A(:, 3)Elements in all rows of column 3  A(2, : )Elements in all columns of row 2  A(:, 2:5) Elements in columns 2 to 5 in all rows  A(2:4, :) Elements in rows 2 to 4 in all columns  A(1:3, 2:4) Elements in rows 1 to 3 and in columns 2 to 4

Extracting Matrix Elements  We can extract a portion of a matrix and assign it to a new variable  new_matrix =matrix( r1 : r2, c1 : c2) r1 is the starting row r2 is the ending row c1 is the starting column c2 is the ending column

Example: Extracting Elements >> A = [ ] A = >> B = A(1:3, 2:4) B =

Example: Extracting Elements >> C = A(1:3, : ) C = >> D = A( :, 2:4) D = Remember

Important Takeaways  An element in a defined vector can be accessed with v(x) - an element in a vector can be defined, or re- defined with v(x)=z  An element in a defined matrix can be accessed with v(x:y)- an element in a matrix can be defined, or re- defined with v(x:y)=z  Strings are lines of text and can be used instead of numerical values - they are defined inside single apostrophes, e.g. ‘Your text here.’

Preview of Next Class  Array Operations Scalar – vector operations Vector – vector operations  Dot operator, when to use it Built-in vector functions  Ex: max, min, mean etc. Examples

What’s Next?  Review today’s Quiz #03  Open the in-class activity from the EEIC website and we will go through it together.  Then, start working on MAT-03 homework.  Before next class, you will read about array operations, this is an introduction of mathematical operations in MATLAB and basics of linear algebra.