Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.

Slides:



Advertisements
Similar presentations
Lecture 4.
Advertisements

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.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
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.
Matrices The Basics Vocabulary and basic concepts.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Part 1 Chapter 2 MATLAB Fundamentals
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
ECON 1150 Matrix Operations Special Matrices
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
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.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
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.
COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating 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.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
Matlab for Engineers Manipulating Matlab Matrices Chapter 4.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Covenant College November 14, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 3: Vectors and Arrays.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
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 Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
MATH 251: MATH SOFTWARE A N I NTRODUCTION TO MATLAB Exercises sheet Al Imam Mohammad Bin Saud Islamic University College of Sciences Department of Mathematics.
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.
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)
Digital Image Processing. Converting between Data Classes The general syntax is B = data_class_name (A) Where data_class_name is one of the names defined.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Project Teams Project on Diffusion will be a TEAM project. 34 Students in class so 6 teams of 5 and one Team of 4. Determine members of team and a team.
An Introduction to Programming in Matlab Emily Blumenthal
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
1 Matrix Math ©Anthony Steed Overview n To revise Vectors Matrices.
Introduction To MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Manipulating MATLAB Matrices Chapter 4
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
Chapter 2 Creating Arrays.
Introduction to Programming for Mechanical Engineers (ME 319)
MATH 493 Introduction to MATLAB
Introduction to MATLAB Lecture 02
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
CSCI N207 Data Analysis Using Spreadsheet
Array and Matrix Functions
Arrays and Matrices in MATLAB
Array Creation ENGR 1181 MATLAB 02.
EECS Introduction to Computing for the Physical Sciences
Presentation transcript:

Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE

Vector – a 1D row or column Several ways of creating a vector: 1. Specifying EACH element Row Vector Elements separated by space or comma. e.g. p = [ ] OR p = [1, 2, 3, 4, 5] p = Column Vector Elements separated by semi- colon (;). e.g. q = [1; 2; 3; 4; 5] q =

Several ways of creating a vector: 2. Specify 1 st term, last term, and spacing(constant) between each element variable_name = [m : q : n] OR variable_name = m : q : n *spacing could be positive or negative.*If spacing is omitted, the default is 1. e.g. a = [1 : 2 : 11] a = b = [12 : -3 : 0] b = c = [1 : 5] ?? c = st term spacing last term

Several ways of creating a vector: 2. Specify 1 st term, last term, and number of terms variable_name = linspace(f, l, n) where f: first term; l : last term ; n : # of terms * If n is omitted, default is 100. e.g. v = linspace(0, 10, 5) v = e.g. w = linspace(1, 50, 1000);

Transpose of a Vector Row and column vectors are transpose of each other. To convert a row vector a to a column vector b, type the transpose operator, a single quote (‘) after the vector name. e.g. >> a = linspace(2,10,5) a = >> b = a' b =

Addressing elements of a vector Addressing a range of elements. For example, addressing elements between and including positions 2 and 5 >> a = v(2:5) a = Re-assign value of 0 to 5th element. >> v(5) = 0 v = >> v(1) * v(5) + v(2) ans = 8

Manipulating Vectors (Adding and Deleting Elements) Add elements to vector by assigning new elements. e.g. >> v = [ ] v = >> v(6) = 6 v = Delete elements from vectors : by assigning element(s) to [] e.g. >> v(5) = [] v = >> v(2:3) = [] v = 1 4 6

Facts about Matrices A 2D array of rows and columns. An mXn matrix has m rows and n columns. Square Matrix : # of rows = # of columns = n(say) Then square matrix would be nXn Identity Matrix : Diagonal elements are ones; non-diagonal elements are zeros Zero matrix : All elements are zeros.

Facts about Matrices Examples of matrices A 3X4 matrix (3 rows and 4 columns) A 3X3 identity matrix A 2X3 zero matrix 0 0 0

Matrices in MATLAB Creating a matrix Variable_name = [r1e1 r1e2..…r1en; r2e1 r2e2… r2en;……. rne1 rne2 …rnen] where r1e1 : row 1 element 1 and so on e.g. a = [1 2 3 ; ; 7 8 9] a = *elements of a matrix(or vector) can be numbers, math expressions, predefined variables and/or functions.

Matrices in MATLAB Zero matrix : zeros(m,n) Ones matrix : ones(m,n) m : # of rows; n : # of columns Identity matrix : eye(n) N : # of rows and columns e.g. >> zeros(2,3) ans = >> eye(3) ans = Transpose of a matrix : rows and columns are interchanged. e.g. >> a = [1 2 3;4 5 6;7 8 9] a = >> b = a' b =

Addressing Matrices By specifying the row and column of the element, we specify its position. Position of 1 st element of a matrix A is A(1,1). A(:,n) Refers to elements in ALL ROWS of column n of matrix A. A(m,:) Elements in ALL COLUMNS and row n. A(:,m:n) Elements in ALL ROWS of columns m through n A(m:n,:) Elements in ALL COLUMNS and rows m through n. A(m:n,p:q) Elements in rows m through n and columns p through q. Addressing Matrices

Manipulating Matrices (Adding and Deleting Elements) Adding elements to matrix: >> V= [1 2 3; 4 5 6] V = >> V(5,:) = ones(1,3) V = Deleting elements from matrix >> V(3:4, :) = [] V = V(2,2) =[] ?? ??? Subscripted assignment dimension mismatch.

Vectors length(v) : returns number of elements in vector v diag(v) : creates a square matrix (from the vector v), with elements of v in the diagonal. e.g. >> v = [1 2 3 ]; >> d = diag(v) d = >> l = length(v) l = 3 Other Built-In Functions Matrices Size(A) : returns row vector [m,n], where m and n are size m X n of the matrix. diag (A) : Creates a vector from the diagonal elements of matrix A. reshape (A,m,n) : creates an m X n matrix from A. elements are taken column after column. A must have m times n elements. e.g. >> A = [1 2 3 ; 4 5 6]; >> s = size(A) s = 2 3 d = (diag(A))' d = 1 5 >> B = reshape(A, 2,3) B =

Strings String is an array of characters. It can include letters, digits, other symbols, and spaces. It’s created by typing characters within single quotes. e.g. ‘MATLAB’, ‘34&g’, ‘h!#ji464’. e. g.>> S = 'It is MATLAB time!!' S = It is MATLAB time!! >> size(S) ans = 1 19 Are n = 123 and s = ‘123’ same?? NO >> size(n) ans = 1 1 >> size(s) ans = 1 3 To create a matrix of strings, each row should have the same number of elements. This can be done using a built-in function called char. Char creates an array with each row having same number of elements. It makes the length of each row same as the longest row by adding spaces. e.g. >> Emp = char('Employee Name', 'Employee Number', 'Department') Emp = Employee Name Employee Number Department