ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

Chapter Matrices Matrix Arithmetic
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
Matrix Definition: An array of numbers in m rows and n colums is called an mxn matrix A square matrix of order n, is an (nxn) matrix.
Mathematics. Matrices and Determinants-1 Session.
MF-852 Financial Econometrics
Maths for Computer Graphics
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Multiple-Subscripted Array
Chapter 2 Section 3 Arithmetic Operations on Matrices.
“No one can be told what the matrix is…
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
Matrices MSU CSE 260.
Matrix Definition A Matrix is an ordered set of numbers, variables or parameters. An example of a matrix can be represented by: The matrix is an ordered.
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
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Matrices Jordi Cortadella Department of Computer Science.
Chapter 8 Matrices and Determinants Copyright © 2014, 2010, 2007 Pearson Education, Inc Matrix Operations and Their Applications.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Matrices A matrix is a table or array of numbers arranged in rows and columns The order of a matrix is given by stating its dimensions. This is known as.
Multidimensional Arrays. 2 Two Dimensional Arrays Two dimensional arrays. n Table of grades for each student on various exams. n Table of data for each.
2009/9 1 Matrices(§3.8)  A matrix is a rectangular array of objects (usually numbers).  An m  n (“m by n”) matrix has exactly m horizontal rows, and.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
The goal is to give an introduction to the mathematical operations with matrices. A matrix is a 2-dimensional arrangement of (real valued) data. The data.
Module #9: Matrices Rosen 5 th ed., §2.7 Now we are moving on to matrices, section 7.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Chapter 6 Matrices and Determinants Copyright © 2014, 2010, 2007 Pearson Education, Inc Matrix Operations and Their Applications.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
Section 4Chapter 4. 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives Solving Systems of Linear Equations by Matrix Methods Define.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Module #9 – Number Theory 1/5/ Algorithms, The Integers and Matrices.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
UNIT 10 Multidimensional Arrays.
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
MATRICES A rectangular arrangement of elements is called matrix. Types of matrices: Null matrix: A matrix whose all elements are zero is called a null.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
MTH108 Business Math I Lecture 20.
Lecture 18: Nested Loops and Two-Dimensional Arrays
13.4 Product of Two Matrices
Properties and Applications of Matrices
12-1 Organizing Data Using Matrices
Two-Dimensional Arrays
1.5 Matricies.
Computer Graphics Matrix
2.2 Introduction to Matrices
Matrices Introduction.
Lecture 13: Two-Dimensional Arrays
Multidimensional array
Lets Play with arrays Singh Tripty
Slides by: Diana Inkpen and Alan Williams
Arrays and Matrices Prof. Abdul Hameed.
Slides by: Diana Inkpen and Alan Williams
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams

Introduction to matrices A matrix is a two dimensional rectangular grid of numbers: The dimensions of the matrix are the numbers of rows and columns (in the above case: row dimension 3, column dimension 3). A value within a matrix is referred to by its row and column indices, in that order. –Math: number rows and columns from 1, from upper left corner In math notation, M 1,2 = 2 –For algorithms (and Java), we will use indices starting from 0, to correspond to the array numbering. In algorithm notation, M[0][1] = 2

Matrix element processing To visit every element of an array, we had to use a loop. To visit every element of a matrix, we need to use a loop inside a loop: –Outer loop: go through each row of a matrix –Inner loop: go through each column within one row of a matrix.

Matrix example Write an algorithm that finds the sum of the upper triangle of a square matrix (i.e. the diagonal and up) M = How do we know if an element of a square matrix is on or above the main diagonal? line_index <= column_index

Matrix example – cont’d GIVENS: M(the matrix) N (the number of rows and columns in the matrix) RESULT: Sum(the sum of all elements in the upper triangle) INTERMEDIATES: R(row index in the matrix) C column index in the matrix) HEADER: Sum  CalculateUpperTriangle(M,N)

Matrix example – cont’d BODY: SUM  0 R  0 R < N ? true C < N ? true R  C ? true SUM  SUM + M[R][C] C  C + 1 false  C  0 false R  R + 1 false

Files needed for this lab WindChill.java –For example 1. ITI1120.java –For reading in arrays for example 1. MatrixLib.java –For reading and printing matrices

Matrix Example 1: Objective 1.Design an algorithm that will create a matrix of wind chill values for various temperatures and wind speeds. –The set of temperatures will be stored in an array –The set of wind speeds will be stored in a second array –An algorithm is already available that calculates the wind chill for a specific temperature and wind speed. 2.Implement the algorithm in Java. For those of you who haven’t yet experienced an Ottawa winter, you may want to keep the results for future use

Given algorithm: Wind Chill calculation Suppose we are given the following algorithm: Chill  WindChill( Temperature, WindSpeed ) that calculates the wind chill for a given temperature in °C, and wind speed in km/hr. Restrictions: –WindSpeed  5.0 km/hr –Temperature should be between -50.0°C and +5.0°C. More info:

Wind Chill Table Design the following algorithm GIVENS: TempArray(an array of temperatures in °C) NT(length of TempArray) SpeedArray(an array of wind speeds in km/hr) NS(length of SpeedArray) RESULTS: ChillTable(a matrix of wind chill values, with NT rows and NS columns) INTERMEDIATES (to be determined) HEADER: ChillTable  WindChillTable( TempArray, NT, SpeedArray, NS)

Implementation in Java Get the following files from the course webpage for lab 9 –WindChill.java –ITI1120.java The file WindChill.java already has a main( ) method as well as an implementation of the method windChill( temperature, windSpeed ) The file WindChill.java contains the method printTable( temperatureArray, speedArray, chillTable ) used to print the matrix.

Sample output ITI 1120 Fall 2010, Lab 9, Example 1 Name: Diana Inkpen, Student# Enter a set of temperatures between -50 and +5: Enter a set of wind speeds: Wind Chill Table Speed: Temp. | 5.0 | | | | | | | | | | Jan. 1994: this value was achieved in Ottawa! (coldest winter since 1948) Lowest value during winters 2003 and 2004 in Ottawa.

Example 2: Matrix Transpose Write an algorithm that will take a matrix of integers A and transpose the matrix to produce a new matrix A T. The transpose of the matrix is such that element a rc in the original matrix will be element a T cr in the transposed matrix. The number of rows in A becomes the number of columns in A T, and the number of columns in A becomes the number of rows in A T. For example:

Translate to Java Translate your matrix transpose algorithm to Java The header of the method should be as follows: public static int[][] transpose( int[][] a ) Get the following file from the labs webpage –MatrixLib.java The file MatrixLib.java has the following useful methods: –Method to read a matrix of integers, of specified dimension. public static int[][] readIntMatrix( int numRows, int numCols ) –Method to print a matrix of integers with nice formatting. public static void printMatrix( int[][] matrix )

Example 3: Matrix Multiplication Suppose that A is an m × n matrix and B is an n × p matrix. The element at row i and column j in A is denoted by a ij. Let C = A × B. Then, C will be an m × p matrix, and for 0 ≤ i < m, and 0 ≤ j < p, Write an algorithm to multiply two given matrices.

Translate to Java Translate your matrix multiplication algorithm to Java The header of the method should be as follows: public static int[][] product( int[][] a, int[][] b ) Use the methods from MatrixLib.java to read and print the matrices.