CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
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 MATLAB
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
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.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Fundamentals of matrices
100’s of free ppt’s from library
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 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.
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,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
13.1 Matrices and Their Sums
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.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
(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.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
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.
MATRICES MATRIX OPERATIONS. About Matrices  A matrix is a rectangular arrangement of numbers in rows and columns. Rows run horizontally and columns run.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
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)
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
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.
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
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.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
ECE 1304 Introduction to Electrical and Computer Engineering
13.4 Product of Two Matrices
ECE 1304 Introduction to Electrical and Computer Engineering
Matrix Operations Free powerpoints at
Matrix Operations.
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,
Lecture: MATLAB Chapter 1 Introduction
Matrix Operations.
Matrix Operations Free powerpoints at
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Matrix Operations Free powerpoints at
MATRICES MATRIX OPERATIONS.
2.2 Introduction to Matrices
Introduction to Matlab
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
3.5 Perform Basic Matrix Operations
CS 111 Introduction to Computing in Engineering and Science
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Chapter 4 Matrices & Determinants
MATRICES MATRIX OPERATIONS.
Presentation transcript:

CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING

Matrices Matlab = Matrix + Laboratory What is the difference between array and matrix? Matrix: mathematical term, 2D only Array: CS term, data structure, can be more than 2D

Arrays A rectangular arrangement of numbers is called an array or matrix.

Vectors and Arrays 1-by-1 arrays: scalars 1-by-N arrays: row vectors N-by-1 arrays: column vectors Row and Column vectors are also sometimes just called vectors In Matlab, all arrays of numbers are called double arrays.

Creating Arrays Horizontal and Vertical Concatenation (ie., “stacking”) –Square brackets, [, and ] to define arrays –Spaces (and/or commas) to separate columns –Semi-colons to separate rows Example >> [ ; ] is the 2-by-3 array If A and B are arrays with the same number of rows, then >> C = [ A B ] is the array formed by stacking A “next to” B Once constructed, C does not “know” that it came from two arrays stacked next to one another. No partitioning information is maintained. If A and B are arrays with the same number of columns, then >> [ A ; B ] is the array formed by stacking A “on top of” B So, [ [ 3 ; 6 ] [ 4 5 ; 7 8 ] ] is equal to [ 3 4 5;6 7 8 ]

Creating special arrays ones(n,m) –a n-by-m double array, each entry is equal to 1 zeros(n,m) –a n-by-m double array, each entry is equal to 0 rand(n,m) –a n-by-m double array, each entry is a random number between 0 and 1. Examples >> A = ones(2,3); >> B = zeros(3,4); >> C = rand(2,5);

: convention The “: (colon) convention” is used to create row vectors, whose entries are evenly spaced. 7:2:18 equals the row vector [ ] If F, J and L are numbers with J>0, F ≤ L, then F:J:L creates a row vector [ F F+J F+2*J F+3*J … F+N*J ] where F+N*J ≤ L, and F+(N+1)*J>L Many times, the increment is 1. Shorthand for F:1:L is F:L

F:J:L with J<0 (decrementing) You can also decrement (ie., J<0), in which case L should be less than F. Therefore 6.5:-1.5:0 is the row vector [ ]

The SIZE command If A is an array, then size(A) is a 1-by-2 array. –The (1,1) entry is the number of rows of A –The (1,2) entry is the number of columns of A If A is an array, then size(A,1) is the number of rows of A size(A,2) is the number of columns of A Example >> A = rand(5,6); >> B = size(A) >> size(A,2)

Accessing single elements of a vector If A is a vector (ie, a row or column vector), then A(1) is its first element, A(2) is its second element,… Example >> A = [ ]; >> A(3) >> Index = 5; >> A(Index) This syntax can be used to assign an entry of A. Recall assignment >> VariableName = Expression An entry of an array may also be assigned >> VariableName(Index) = Expression So, change the 4’th entry of A to the natural logarithm of 3. >> A(4) = log(3);

Accessing elements and parts of arrays If M is an array, then M(3,4) is –the element in the (3 rd row, 4 th column) of M If M is an array, then M([1 4 2],[5 6]) –is a 3-by-2 array, consisting of the entries of M from rows [1, 4 and 2] columns [5 and 6] In an assignment >> M(RIndex,CIndex) = Expression the right-hand side expression should be a scalar, or the same size as the array being referenced by M(RIndex,CIndex) Do some examples

Name = ‘A’ Size = [2 3]; Data = Workspace: Base Layout in memory The entries of a numeric array in Matlab are stored together in memory in a specific order. >> A = [ ; ]; represents the array Somewhere in memory, Matlab has

RESHAPE RESHAPE changes the size, but not the values or order of the data in memory. >> A = [ ; ]; >> B = reshape(A,[3 2]); The result (in memory is) Name = ‘A’ Size = [2 3]; Data = Name = ‘B’ Size = [3 2]; Data = while B is the array So, A is the array

END Suppose A is an N-by-M array, and a reference of the form A(RIndex,CIndex) Any occurence of the word end in the RIndex is changed (automatically) to N Any occurence of the word end in the CIndex is changed (automatically) to M Example: >> M = rand(4,5); >> M(end,end) >> M([1 end],[end-2:end])

: as a row or column index Suppose A is an N-by-M array, and a reference of the form A(RIndex,CIndex) If RIndex is a single colon, :, then RIndex is changed (automatically) to 1:N (every row) If C Index is a single colon, :, then C Index is changed (automatically) to 1:M (every column) Example: >> M = rand(4,5); >> M(:,[1 3 5])

Matrix operations Add, Subtract, Multiply examples A+B A-B A*B Do they have to have the same dimensions? What about division? A./B

Unary Numeric Operations on double Arrays Unary operations involve one input argument. Examples are: –Negation, using the “minus” sign –Trig functions, sin, cos, tan, asin, acos, atan,… –General rounding functions, floor, ceil, fix, round –Exponential and logs, exp, log, log10, sqrt –Complex, abs, angle, real, imag Example: If A is an N1-by-N2-by-N3-by-… array, then B = sin(A); is an N1-by-N2-by-N3-by-… array. Every entry of B is the sin of the corresponding entry of A. The “for”-loop that cycles the calculation over all array entries is an example of the vectorized nature of many Matlab builtin functions

QUESTIONS??