EECS Introduction to Computing for the Physical Sciences

Slides:



Advertisements
Similar presentations
Maths for Computer Graphics
Advertisements

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.
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.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
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.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
Working with Arrays in MATLAB
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.
(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.
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.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Introduction To MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Linear Algebra Review.
Introduction to MATLAB for Engineers, Third Edition
ECE 1304 Introduction to Electrical and Computer Engineering
Linear Algebra review (optional)
Chapter 7 Matrix Mathematics
Chapter 2 Creating Arrays.
EGR 115 Introduction to Computing for Engineers
Matrix Operations SpringSemester 2017.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
7.3 Matrices.
MATH 493 Introduction to MATLAB
Introduction to Matrices
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Digital Image Processing
CSCI N207 Data Analysis Using Spreadsheet
JavaScript Arrays.
Section 2.4 Matrices.
2.2 Introduction to Matrices
Communication and Coding Theory Lab(CS491)
Arrays and Matrices in MATLAB
Array Creation ENGR 1181 MATLAB 02.
INTRODUCTION TO MATLAB
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Introduction to Matlab
Matlab Training Session 2: Matrix Operations and Relational Operators
Linear Algebra review (optional)
Matrices in MATLAB Dr. Risanuri Hidayat.
Matrix Operations Ms. Olifer.
Matrix Operations SpringSemester 2017.
Arrays in MatLab Arrays can have any number dimensions
Working with Arrays in MATLAB
Introduction to Matrices
Announcements.
Presentation transcript:

EECS 1541 -- Introduction to Computing for the Physical Sciences Week 2: Vectors and Matrices (Part I) READING: 2.1.1 – 2.1.2

EECS 1541 -- Introduction to Computing for the Physical Sciences Arrays Vectors and matrices are used to store set of values. Like an array, vectors and matrices can be visualized as a table of values. The dimensions of an array are r by c, where r is the number of rows and c is the number of columns.

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Scalars A scalar in MATLAB is an array of size 1 x 1 Example: a variable that stores a single value is an example of a scalar >> z = 6 z = 6

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Vectors A vector in MATLAB is equivalent to a 1-dimensional array where one of the dimensions is 1 There are: row vectors and column vectors row vector column vector

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors A row vector in MATLAB is an 1 x n array Example of an 1 x 4 array that consists of 4 elements: >> v = [1 2 3 4] v = 1 2 3 4 >> v = [1, 2, 3, 4] v = 1 2 3 4

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors MATLAB workspace after a row vector has been assigned:

Row Vectors: Colon Operator  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator The colon operator can be used to iterate values in a vector Format: Vector_variable_name = initial value : step: final value step size Example 1: >> v = 1:2:7 v = 1 3 5 7

Row Vectors: Colon Operator  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator Example 2: If “step” is omitted, the default step size is 1 >> v = 1:6 v = 1 2 3 4 5 6

Row Vectors: Colon Operator  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator Example 3: >> v = 1:3:12 v = Adding 3 to 10 would go beyond 12, so the vector stops at 10 1 4 7 10

Row Vectors: Colon Operator  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors: Colon Operator Example 4: >> start = 20; >> step = -3; >> final = 6; v = start:step:final v = Observe that the final value is not guaranteed to be at the end of the vector 20 17 14 11 8

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Row Vectors: Linspace linspace(x,y,n) function creates a vector with n values in the inclusive range from x to y. It creates a vector with n values that are linearly spaced. If n is omitted, the default is 100 points If n = 1, linspace(x,y,1) returns y Example : >> v = linspace(1,5,3) v = 1 3 5

Concatenating the vectors  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Concatenating the vectors Vectors variables can be created using existing variables Example : >> v1 = 2:5; >> v2 = 1:3; >> v_total = [v1 v2] v_total = 2 3 4 5 1 2 3 From v1 From v2

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Column Vectors A column vector in MATLAB is a n x 1 array A column vector can be created directly by entering the values of the vector inside a pair of square brackets with the values separated by semi-colons Example : >> v = [2; 4; 6; 8] v = 2 4 6 8

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Column Vectors Any row vector created using any method can be transposed to result in a column vector Example : >> r_v = 2:5; >> c_v = r_v’ c_v = 2 3 4 5

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Column Vectors A column vector can also be created using the colon notation like the following example: Example : >> v = 1:5; >> c = v(:) c = 1 2 3 4 5

Number of elements in a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Number of elements in a vector The function length will return the number of elements in a vector (i.e. both row vectors and column vectors) Example : >> v = 2:3:9 v = 2 5 8 >> length(v) ans = 3

Number of elements in a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Number of elements in a vector Example : >> v = 3:7; >> w = v’’; >> length(w) ans = 5

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector The elements in a vector are numbered sequentially; like an array, each element number is called the index In MATLAB, the indices start at 1 index element To access a particular element in a vector, enter the name of the vector variable and the index in parentheses vector_name()

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = -10 -8 -6 -4 -2 0 >> v(2) ans = index is 2, so get the value of the second element in v -8

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = -10 -8 -6 -4 -2 0 >> v(5) ans = -2 index is 5, so get the value of the fifth element in v

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = -10 -8 -6 -4 -2 0 >> v(end) ans = “end” refers to the last element in the vector

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = -10 -8 -6 -4 -2 0 >> v(end-1) ans = -2

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = -10 -8 -6 -4 -2 0 >> v(2:4) ans = You can return a range of elements by inputting the indices -8 -6 -4

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> v = -10:2:0 v = -10 -8 -6 -4 -2 0 >> v([1 3]) ans = The index can be a vector of indices In this example, we want to obtain the first and the third element of the vector -10 -6

Indexing elements of a vector  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Indexing elements of a vector Example : >> a = linspace(2,6,5); >> b = a’; >> b(length(b)-3) ans = 3 Based on this example, can you think of a general formula on how MATLAB compute all the values in linspace (when n is not equal to 1)?

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Magnitude of a vector Magnitude of a vector (v) in n-dimensional can be computed by: Magnitude of a vector can be computed using the norm function For example, the magnitude of the vector (1,1,1) can be computer in MATLAB: >> v = [1 1 1]; >> v_mag = norm(v) v_mag = 1.7321

EECS 1541 -- Introduction to Computing for the Physical Sciences  returns an n-by-n matrix of pseudorandom normal values EECS 1541 -- Introduction to Computing for the Physical Sciences Magnitude of a vector You can select the elements in a vector and find out the corresponding magnitude using the norm function >> v = [1 1 1]; Example, >> v = [1 1 1]; >> v_mag = norm(v(1:2)) v_mag = 1.4142