Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Lesson 12.2 Matrix Multiplication. 3 Row and Column Order The rows in a matrix are usually indexed 1 to m from top to bottom. The columns are usually.
CSNB143 – Discrete Structure
Chapter 4 Systems of Linear Equations; Matrices Section 6 Matrix Equations and Systems of Linear Equations.
Matrices CSE, POSTECH.
Arrays.
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
Matrix Multiplication To Multiply matrix A by matrix B: Multiply corresponding entries and then add the resulting products (1)(-1)+ (2)(3) Multiply each.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
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.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Matrix table of values
Matrices MSU CSE 260.
MATRICES. Matrices A matrix is a rectangular array of objects (usually numbers) arranged in m horizontal rows and n vertical columns. A matrix with m.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Chapter 1: Matrices Definition 1: A matrix is a rectangular array of numbers arranged in horizontal rows and vertical columns. EXAMPLE:
1 Chapter 3 Matrix Algebra with MATLAB Basic matrix definitions and operations were covered in Chapter 2. We will now consider how these operations are.
Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH. 2 2 Any real-life matrix you know?
SNU IDB Lab. Ch8. Arrays and Matrices © copyright 2006 SNU IDB Lab.
Lecture 7 Matrices CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Chapter 4 – Matrix CSNB 143 Discrete Mathematical Structures.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
If A and B are both m × n matrices then the sum of A and B, denoted A + B, is a matrix obtained by adding corresponding elements of A and B. add these.
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.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
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.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Arrays.
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE.
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.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Chapter 1 Section 1.6 Algebraic Properties of Matrix Operations.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
CS 285- Discrete Mathematics Lecture 11. Section 3.8 Matrices Introduction Matrix Arithmetic Transposes and Power of Matrices Zero – One Matrices Boolean.
Introduction Types of Matrices Operations
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
MTH108 Business Math I Lecture 20.
Chapter 4 Systems of Linear Equations; Matrices
CSE 504 Discrete Mathematics & Foundations of Computer Science
CSNB 143 Discrete Mathematical Structures
13.4 Product of Two Matrices
Properties and Applications of Matrices
nhaa/imk/sem /eqt101/rk12/32
Discrete Structures – CNS2300
1.5 Matricies.
Arrays.
Matrix Multiplication
Reminder: Array Representation In C++
بردارها و ماتريس ها ساختمان داده ها
Reminder: Array Representation In C++
Matrices Introduction.
CS100: Discrete structures
Multidimensional array
Lets Play with arrays Singh Tripty
Multi-Dimensional Arrays
Arrays.
Arrays.
3.6 Multiply Matrices.
Section 9.4 Matrix Operations
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
Reminder: Array Representation In C++
Arrays and Matrices Prof. Abdul Hameed.
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Arrays and Matrices CSE, POSTECH

2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example of tabular data and is often represented as a 2-dimensional array – Matrices are normally indexed beginning at 1 rather than 0 – Matrices also support operations such as add, multiply, and transpose, which are NOT supported by C++’s 2D array

3 3 Introduction It is possible to reduce time and space using a customized representation of multidimensional arrays This chapter focuses on – Row- and column-major mapping and representations of multidimensional arrays – the class Matrix – Special matrices Diagonal, tridiagonal, triangular, symmetric, sparse

4 1D Array Representation in C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations location(x[i]) = start + i Memory abcd start

5 Space Overhead space overhead = 4 bytes for start (excludes space needed for the elements of x) Memory abcd start

6 2D Arrays The elements of a 2-dimensional array a declared as: int a[3][4]; may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

7 Rows of a 2D Array a[0][0] a[0][1] a[0][2] a[0][3] row 0 a[1][0] a[1][1] a[1][2] a[1][3] row 1 a[2][0] a[2][1] a[2][2] a[2][3] row 2

8 Columns of a 2D Array a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] column 0column 1column 2column 3

9 2D Array Representation in C++ view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a, b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 4 1D arrays 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l

10 2D Array Representation in C++ abcd efgh ijkl x[] 4 separate 1-dimensional arrays space overhead = overhead for 4 1D arrays = 4 * 4 bytes = 16 bytes = (number of rows + 1) x 4 bytes

11 Array Representation in C++ This representation is called the array-of-arrays representation. Requires contiguous memory of size 3, 4, 4, and 4 for the 4 1D arrays. 1 memory block of size number of rows and number of rows blocks of size number of columns abcd efgh ijkl x[]

12 Row-Major Mapping Example 3 x 4 array: a b c d e f g h i j k l Convert into 1D array y by collecting elements by rows. Within a row elements are collected from left to right. Rows are collected from top to bottom. We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l} row 0 row 1row 2…row i

13 Locating Element x[i][j] assume x has r rows and c columns each row has c elements i rows to the left of row i so ic elements to the left of x[i][0] x[i][j] is mapped to position ic + j of the 1D array row 0 row 1row 2…row i 0c2c3cic

14 Space Overhead 4 bytes for start of 1D array + 4 bytes for c (number of columns) = 8 bytes Note that we need contiguous memory of size rc. row 0row 1row 2…row i

15 Column-Major Mapping a b c d e f g h i j k l Convert into 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. We get y = {a, e, i, b, f, j, c, g, k, d, h, l}

16 Row- and Column-Major Mappings 2D Array int a[3][6]; a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5] a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] a[2][5]

17 Row- and Column-Major Mappings Row-major order mapping functions map(i 1,i 2 ) = i 1 u 2 +i 2 for 2D arrays map(i 1,i 2,i 3 ) = i 1 u 2 u 3 +i 2 u 3 +i 3 for 3D arrays What is the mapping function for Figure 7.2(a)? map(i 1,i 2 ) = 6i 1 +i 2 map(2,3) = ? Column-major order mapping functions // do this as an exercise

18 Irregular 2D Arrays Irregular 2-D array: the length of rows is not required to be the same x[] 789l0

19 Creating and Using Irregular 2D Arrays // declare a two-dimensional array variable // and allocate the desired number of rows int ** irregularArray = new int*[numberOfRows]; // now allocate space for elements in each row for (int i = 0; i < numberOfRows; i++) irregularArray[i] = new int [length[i]]; // use the array like any regular array irregularArray[2][3] = 5; irregularArray[4][6] = irregularArray[2][3]+2; irregularArray[1][1] += 3;

20 Matrices m x n matrix is a table with m rows and n columns. M(i,j) denotes the element in row i and column j. Common matrix operations – transpose – addition – multiplication

21 Matrix Operations Transpose – The result of transposing an m x n matrix is an n x m matrix with property: M T (j,i) = M(i,j), 1 <= i <= m, 1 <= j <= n Addition – The sum of matrices is only defined for matrices that have the same dimensions. – The sum of two m x n matrices A and B is an m x n matrix with the property: C(i,j) = A(i,j) + B(i,j), 1 <= i <= m, 1 <= j <= n

22 Matrix Operations Multiplication – The product of matrices A and B is only defined when the number of columns in A is equal to the number of rows in B. – Let A be m x n matrix and B be a n x q matrix. A*B will produce an m x q matrix with the following property: C(i,j) = Σ(k=1…n) A(i,k) * B(k,j) where 1 <= i <= m and 1 <= j <= q Read Example 7.2

23 A Matrix Class There are many possible implementations for matrices. // use a built-in 2 dimensional array T matrix[m][n] // use the Array2D class Array2D matrix(m,n) // or flatten the matrix into a one-dimensional array template class Matrix { private:int rows, columns; T *data; };

24 Shortcomings of using a 2D Array for a Matrix Indexes are off by 1. C++ arrays do not support matrix operations such as add, transpose, multiply, and so on. – Suppose that x and y are 2D arrays. Cannot do x + y, x – y, x * y, etc. in C++. We need to develop a class matrix for object- oriented support of all matrix operations. See Programs Read Sections