Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers.

Similar presentations


Presentation on theme: "Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers."— Presentation transcript:

1

2 Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers

3 Two-Dimensional Arrays 6154 1192 7131 5812 row 0 row 1 row 2 row 3 Example: int twoDArr[4][3];// declares a 2-D array with 4 rows and 3 columns cout << twoDArr[0][2];// prints the value in row 0, col 2 // The value 4 in this example col 0col 1col 2 Access elements using two indices or subscripts, the first for the row and the second for the column

4 Assigning data to 2-D Arrays  Initialization: char table[2][3]{ {'a','g','k'}{'c', 'm', 'h'}};  Assignment using nested for loops for (int i=0; i > table[i][j]; 'a''g''k' 'c''m''h'

5 Functions and 2-D Arrays  Default is call-by reference when passing 2-D array to a function  All sizes of the array, except the leftmost one, must be specified. The leftmost one may be []  Function prototype example: int rowAverage(int Arr[][4], int whichRow);  Array declaration in main: int table [5][4];  Function invocation example: Ave = rowAverage(table, 3);

6 Matrices  Mathematical Matrix can be implemented as 2-D array be careful translating between C++ indices and matrix subscripts.

7 Determinant  Used in computing inverses of matrices and solving systems of simultaneous equations.  Determinant of 2x2 matrix:  Determinant of 3x3 matrix:

8 Transpose of a matrix  Rows of original matrix become columns in the transposed matrix.

9 // This function generates a matrix transpose. void transpose (int b[] [ncols], int bt[] [nrows]) { for (int i=0; i <= nrows-1; i++) { for (int j=0; j <= ncols-1; j++) { bt[j] [i] = b[i] [j]; } return; }

10 Matrix Addition and Subtraction  Matrix addition (or subtraction) adds (or subtracts) corresponding elements of the two matrices producing a third matrix of the same size.  The original two matrices must also be of the same size.

11 Practice!  Given the following matrices, what is A+B ?

12 Matrix Multiplication A*B  The number of columns in A must be the same as the number of rows in B.  The resulting matrix has the number of rows in A and the number of columns in B.  Element c ij, where C=A*B, is:

13 Practice!  Given the following matrices, what is A*B ?

14 // This function performs a matrix multiplication. // of two NxN matrices. void matrix_mult (int a[] [N], int b[] [N], int c[] [N]) { for (int i=0; i <= N-1; i++) { for (int j=0; j <= N-1; j++) { c[i] [j] = 0; for (int k=0; k <= N-1; k++) { c[i] [j] += a[i] [k]*b[k] [j]; } return; }

15 Solving System of Equations  Cramer’s Rule  Matrix Inversion  Gaussian Elimination  Etc.

16 Method of Matrix Inversion Rewriting in matrix form Ay = b,

17 1) Find the cofactor matrix of A The minor of an element a ij of a matrix A is the determinant of a matrix formed by removing the i th row and j th column of the matrix A.

18 2. Find the determinant of A Det (A) = sum of the products between every element in a given row or column and its cofactor

19 3. Find the adjoint matrix of A. The adjoint matrix of A is the matrix transpose of the cofactor matrix of A. 4. Find the matrix inverse of A.

20 5. The solutions are given as


Download ppt "Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers."

Similar presentations


Ads by Google