Arrays Revisited Selim Aksoy Bilkent University Department of Computer Engineering

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
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.
Maths for Computer Graphics
Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering
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.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Review Selim Aksoy Bilkent University Department of Computer Engineering
Vectors and Plotting Selim Aksoy Bilkent University Department of Computer Engineering
Week 3 Last week Vectors Array Addressing Mathematical Operations Array Multiplication and Division Identity Matrix Inverse of a Matrix Element by Element.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Additional Data Types: 2-D Arrays, Logical Arrays Selim Aksoy Bilkent University Department of Computer Engineering
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.
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Matrix Mathematics in MATLAB and Excel
Intro to Matrices Don’t be scared….
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Introduction to MATLAB MATLAB is all of the following: 1.Computational environment 2.Plotting software 3.Programming language Typical applications: 1.Calculations.
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 Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
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.
Array Math.
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,
Array Addition  Two arrays can be added if and only if both arrays have exactly the same dimensions.  Assuming the dimension requirement is satisfied,
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Chapter 2 Numeric, Cell, and Structure Arrays. Physics Connection - Specification of a position vector using Cartesian coordinates. Figure 2.1–1 2-2 The.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
Lecture 28: Mathematical Insight and Engineering.
Matrices. Definitions  A matrix is an m x n array of scalars, arranged conceptually as m rows and n columns.  m is referred to as the row dimension.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Introduction to MATLAB
Vectors Lecture 11: Supporting Material Dr Kathryn Merrick Tuesday 7 th April, 2009.
Working with Arrays in MATLAB
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.
ES 240: Scientific and Engineering Computation. Chapter 8 Chapter 8: Linear Algebraic Equations and Matrices Uchechukwu Ofoegbu Temple University.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
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.
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB
MT411 Robotic Engineering Asian Institution of Technology (AIT) Chapter 1 Introduction to Matrix Narong Aphiratsakun, D.Eng.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
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
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.
ENG College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Unit 3: Matrices. Matrix: A rectangular arrangement of data into rows and columns, identified by capital letters. Matrix Dimensions: Number of rows, m,
Matrices. Matrix - a rectangular array of variables or constants in horizontal rows and vertical columns enclosed in brackets. Element - each value in.
To add, subtract, multiply, and divide, absolutely follow the mathematical rules. 1. All the rules 2. The element-per-element operator 3. Ex1: graphing.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
ECE 1304 Introduction to Electrical and Computer Engineering
L – Modeling and Simulating Social Systems with MATLAB
Linear Algebraic Equations and Matrices
الوحدة السابعة : المصفوفات . تنظيم البيانات فى مصفوفات . الوحدة السابعة : المصفوفات . تنظيم البيانات فى مصفوفات . 1 جمع المصفوفات وطرحها.
Unit 3: Matrices
Presentation transcript:

Arrays Revisited Selim Aksoy Bilkent University Department of Computer Engineering

Fall 2004CS 1112 Initializing Vectors colon operator x = 1:2:10 x = linspace(x1,x2,N) generates a row vector of N linearly equally spaced points between x1 and x2 x = linspace(10,20,5) x = logspace(x1,x2,N) can be used for logarithmically equally spaced points

Fall 2004CS 1113 Vector Input to Functions You can call many built-in functions with array inputs The function is applied to all elements of the array The result is an array with the same size as the input array

Fall 2004CS 1114 Vector Input to Functions Examples: x = [ ]; abs(x) ans = sin( [ 0 pi/6 pi/4 pi/3 pi/2 ] ) ans = a = 1:5; log(a) ans =

Fall 2004CS D Arrays a = [ 1:2:7; 10:2:16 ] a = [ x, y ] = size(a) x = 2 y = 4 a(:) ans = Recall matrices and subarrays

Fall 2004CS D Arrays Adding the elements of a matrix [r,c] = size(a); s = 0; for ii = 1:r, for jj = 1:c, s = s + a(ii,jj); end end

Fall 2004CS D Arrays a = [ 1:2:7; 10:2:16 ]; sum(a) ans = sum(a,1) ans = sum(a,2) ans = sum(sum(a)) ans = 68 sum(a(:)) ans = 68 Adding the elements of a matrix (cont.)

Fall 2004CS D Arrays Finding the maximum value in each row [r,c] = size(a); f = zeros(r,1); for ii = 1:r, m = a(ii,1); for jj = 2:c, if ( m < a(ii,jj) ), m = a(ii,jj); end end f(ii) = m; end

Fall 2004CS D Arrays a = [ 1:2:7; 10:2:16 ]; max(a) ans = max(a,[],1) ans = max(a,[],2) ans = 7 16 max(max(a)) ans = 16 max(a(:)) ans = 16 Finding the maximum value in each row (cont.)

Fall 2004CS D Arrays Replace elements that are greater than t with the number t [r,c] = size(a); for ii = 1:r, for jj = 1:c, if ( a(ii,jj) > t ), a(ii,jj) = t; end end end

Fall 2004CS D Arrays Replace elements that are greater than t with the number t (cont.) a( a > t ) = t (“a > t” is a logical array)

Fall 2004CS Array Operations Scalar-array operations x = 1:5 x = y = 2 * x  scalar multiplication y = z = x + 10  scalar addition z =

Fall 2004CS Array Operations Array-array operations (element-by-element operations) x = [ ]; y = [ ]; z = x + y z = z = x.* y z = z = x./ y z =

Fall 2004CS Array Operations Array-array operations (element-by-element operations) z = x.^ y z = Use.*,./,.^ for element-by-element operations Array dimensions must be the same

Fall 2004CS Loops vs. Vectorization Problem: Find the maximum value in a vector Soln. 1: Write a loop Soln. 2: Use the built-in function “max” Use built-in MATLAB functions as much as possible instead of reimplementing them

Fall 2004CS Loops vs. Vectorization %Compares execution times of loops and vectors % %by Selim Aksoy, 7/3/2004 %Create a vector of random values x = rand(1,10000); %Find the maximum value using a loop tic; %reset the time counter m = 0; for ii = 1:length(x), if ( x(ii) > m ), m = x(ii); end t1 = toc; %elapsed time since last call to tic %Find the maximum using the built-in function tic; %reset the time counter m = max(x); t2 = toc; %elapsed time since last call to tic %Display timing results fprintf( 'Timing for loop is %f\n', t1 ); fprintf( 'Timing for built-in function is %f\n', t2 );

Fall 2004CS Loops vs. Vectorization Problem: Compute 3x 2 +4x+5 for a given set of values Soln. 1: Write a loop Soln. 2: Use 3*x.^2 + 4*x + 5 Allocate all arrays used in a loop before executing the loop If it is possible to implement a calculation either with a loop or using vectors, always use vectors

Fall 2004CS Loops vs. Vectorization %Compares execution times of loops and vectors % %by Selim Aksoy, 7/3/2004 %Use a loop tic; %reset the time counter clear y; for x = 1:10000, y(x) = 3 * x^2 + 4 * x + 5; end t1 = toc; %elapsed time since last call to tic %Use a loop again but also initialize the result vector tic; %reset the time counter clear y; y = zeros(1,10000); for x = 1:10000, y(x) = 3 * x^2 + 4 * x + 5; end t2 = toc; %elapsed time since last call to tic %Use vector operations tic; %reset the time counter clear y; x = 1:10000; y = 3 * x.^2 + 4 * x + 5; t3 = toc; %elapsed time since last call to tic %Display timing results fprintf( 'Timing for uninizialed vector is %f\n', t1 ); fprintf( 'Timing for inizialed vector is %f\n', t2 ); fprintf( 'Timing for vectorization is %f\n', t3 );

Fall 2004CS Matrix Operations Transpose operator  ' u = [ 1:3 ]' u = v = [ u u ] v = v = [ u'; u' ] v =

Fall 2004CS Matrix Operations Matrix multiplication: C = A * B If A is a p-by-q matrix B is a q-by-r matrix then C will be a p-by-r matrix where

Fall 2004CS Matrix Operations Matrix multiplication: C = A * B

Fall 2004CS Matrix Operations Examples a = [ 1 2; 3 4 ] a = b = [ -1 3; 2 -1 ] b = a.* b ans = a * b ans =

Fall 2004CS Matrix Operations Examples a = [ 1 4 2; 5 7 3; ] a = b = [ 6 1; 2 5; 7 3 ] b = c = a * b c = d = b * a ??? Error using ==> * Inner matrix dimensions must agree.

Fall 2004CS Matrix Operations Identity matrix: I A * I = I * A = A Examples a = [ 1 4 2; 5 7 3; ]; I = eye(3) I = a * I ans =

Fall 2004CS Matrix Operations Inverse of a matrix: A -1 A A -1 = A -1 A = I Examples a = [ 1 4 2; 5 7 3; ]; b = inv(a) b = a * b ans =

Fall 2004CS Matrix Operations Matrix left division: C = A \ B Used to solve the matrix equation A X = B where X = A -1 B In MATLAB, you can write x = inv(a) * b x = a \ b (second version is recommended)

Fall 2004CS Matrix Operations Example: Solving a system of linear equations A = [ ; 2 8 2; ]; B = [ ]'; X = A \ B X =

Fall 2004CS Matrix Operations Matrix right division: C = A / B Used to solve the matrix equation X A = B where X = B A -1 In MATLAB, you can write x = b * inv(a) x = b / a (second version is recommended)