Array Accessing and Strings ENGR 1181 MATLAB 3

Similar documents
Array Accessing and Strings ENGR 1187 MATLAB 3

Array Creation ENGR 1181 MATLAB 2

Array Creation ENGR 1187 MATLAB 2

ENGR 1181 MATLAB 02: Array Creation

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Ch.5. Loops. (a.k.a. repetition or iteration)

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

Matlab- Command Window Operations, Scalars and Arrays

ENGR 1181 MATLAB 4: Array Operations

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

ARRAY VARIABLES (ROW VECTORS)

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

ENGR 1181 MATLAB 09: For Loops 2

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Mathematical Operations with Arrays and Matrices

Introduction to Engineering gii

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

3. Replace any row by the sum of that row and a constant multiple of any other row.

Matlab Programming Introduction 1 2

agenda 10 MIN Go over benchmark 30 MIN Unit 2 Day 1 Notes 5 MIN Mastery Work

Introduction to Matlab. By: Hossein Hamooni Fall 2014

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

CME 192: Introduction to Matlab

Matrices 4: use of MATLAB

One-dimensional Array

MATLAB TUTORIAL WORKSHEET

Introduction to MATLAB for Engineers, Third Edition

Introduction to MATLAB

Dynamics and Vibrations Mupad tutorial

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

Objectives of this lesson

MBI REU Matlab Tutorial

Homework #2: Introduction to Images Due 4 th Week of Spring 2018 at the start of lab CSE 7, Spring 2018

Chapter 2. MATLAB Fundamentals

Introduction to MATLAB

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Constraint Based Modeling Geometric and Dimensional. ENGR 1182 SolidWorks 03

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

SF1901 Probability Theory and Statistics: Autumn 2016 Lab 0 for TCOMK

EOSC 352 MATLAB Review

Maths for Signals and Systems Linear Algebra in Engineering. Some problems by Gilbert Strang

MATLAB An introductory tutorial

Computer Packet 1 Row Operations + Freemat

To start using Matlab, you only need be concerned with the command window for now.

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Matlab and Coordinate Systems

What We Will Learn Today

Array programming languages

Introduction to MATLAB 7 for Engineers

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

XPM 2D Transformations Week 2, Lecture 3

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

Introduction to MATLAB

EGR 111 Introduction to MATLAB

Math 2331 Linear Algebra

Programming Exercise 3: Multi-class Classification and Neural Networks

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Introduction to MatLab. Introduction to MatLab K. Craig 1

Programming in Mathematics. Mili I. Shah

Using the fprintf command to save output to a file.

XPM 2D Transformations Week 2, Lecture 3

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Dr Richard Greenaway

Matlab Tutorial 1: Working with variables, arrays, and plotting

MATLAB Demo. Preliminaries and Getting Started with Matlab

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

MATLAB Modul 3. Introduction

Why use MATLAB? Mathematcal computations. Used a lot for problem solving. Statistical Analysis (e.g., mean, min) Visualisation (1D-3D)

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

How to program with Matlab (PART 1/3)

A gentle introduction to Matlab

OpenCV. Basics. Department of Electrical Engineering and Computer Science

MATLAB Tutorial III Variables, Files, Advanced Plotting

McTutorial: A MATLAB Tutorial

Column and row space of a matrix

EN 001-4: Introduction to Computational Design. Matrices & vectors. Why do we care about vectors? What is a matrix and a vector?

Computational Finance

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

Matrix Multiplication

Introduction to Matlab

A very brief Matlab introduction

! The MATLAB language

Dr. Nahid Sanzida b e. uet .ac.

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Slightly revised version of Scilab and Matrices by David Arnold, College of the Redwoods, August 17, 1998.

PSY8219 : Week 2. Homework 1 Due Today. Homework 2 Due September 12. Readings for Today Attaway Chapters 2, 7, and 8

Programming Exercise 1: Linear Regression

1 Introduction to Matlab

ENGR 1181 MATLAB 05: Input and Output

Xiang Li. Temple University. September 6, 2016

Graph drawing in spectral layout

Transcription:

Array Accessing and Strings ENGR 1181 MATLAB 3

Array Accessing In The Real World 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.

Today's Learning Objectives After today s class, students will be able to: Demonstrate proper notation for accessing elements from previously assigned onedimensional arrays (e.g., single elements, list of elements) and two-dimensional arrays (e.g., those with rows and columns). Explain that a string is a one dimensional array and can be used the same way as numeric arrays.

What is Addressing? 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

Vector Addressing Example Define a vector with 9 elements: >> v = [ 12 15 18 21 24 27 30 33 36]; 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= [12 15 18 21 24 27 30 33 36] >> v(6) = 90; >> v v = 12 15 18 21 24 90 30 33 36

Addressing Column Vectors Addressing 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, many built-in functions we can use with vectors max() min() sum() length() etc.

Vector Functions length() gives us the number of elements in a vector >> fun = [4 6 8 10 12]; >> length(fun) ans = 5

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

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

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 = [ 1 3 5 7 9 11 13 15 ]; Select elements 3 through 7 in 'vec': >> vec(3:7) vec = 5 7 9 11 13

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

Vector Modifications We can add elements to any existing vector. Recall that 'vec' has 8 elements: vec = [ 1 3 5 7 9 11 13 15 ] >> vec(9: 12)= [ 2 4 6 8] vec = 1 3 5 7 9 11 13 15 2 4 6 8

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

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 = [ 2 3 4 5 ; 1 6 8 9] data = 2 3 4 5 1 6 8 9 >> 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 = 2 3 4 5 1 6 8 9 >> 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) A(2:4, :) A(1:3, 2:4) Elements in columns 2 to 5 in all rows Elements in rows 2 to 4 in all columns 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 = [ 1 3 5 7 2 4 6 8 3 6 9 12 4 8 12 16] A = 1 3 5 7 2 4 6 8 3 6 9 12 4 8 12 16 >> B = A(1:3, 2:4) B = 3 5 7 4 6 8 6 9 12

Example: Extracting Elements >> C = A(1:3, : ) C = 1 3 5 7 2 4 6 8 3 6 9 12 >> D = A( :, 2:4) D = 3 5 7 4 6 8 6 9 12 8 12 16 Remember

Important Takeaways An element in a defined vector can be accessed with v(x) - an element in a vector can be defined, or redefined 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 redefined 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.