¡Descarga Introducción a la Programación: Matrices y Vectores Multidimensionales y más Apuntes en PDF de Ingeniería Infórmatica solo en Docsity!
Introduction to Programming
(in C++)
Multi-dimensional vectors
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. LSI, UPC
Matrices
- (^) A matrix can be considered a two-dimensional
vector, i.e. a vector of vectors.
// Declaration of a matrix with 3 rows and 4 columns vector< vector > my_matrix(3,vector(4)); // A more elegant declaration typedef vector Row; // One row of the matrix typedef vector Matrix; // Matrix: a vector of rows Matrix my_matrix(3,Row(4)); // The same matrix as above 3 8 1 0 5 0 6 3 7 2 9 4 my_matrix:
n -dimensional vectors
- (^) Vectors with any number of dimensions can
be declared:
typedef vector Dim1; typedef vector Dim2; typedef vector Dim3; typedef vector Matrix4D; Matrix4D my_matrix(5,Dim3(i+1,Dim2(n,Dim1(9))));
Sum of matrices
- (^) Design a function that calculates the sum of
two n×m matrices.
typedef vector< vector > Matrix; Matrix matrix_sum(const Matrix& a, const Matrix& b);
Sum of matrices (by rows)
typedef vector< vector > Matrix; Matrix matrix_sum(const Matrix& a, const Matrix& b) { // Pre: a and b are non-empty matrices // and are the same size // Post: returns a+b (sum of matrices) int nrows = a.size(); int ncols = a[0].size(); Matrix c(nrows, vector(ncols)); for (int i = 0; i < nrows; ++i) { for (int j = 0; j < ncols; ++j) { c[i][j] = a[i][j] + b[i][j]; } } return c; }
Sum of matrices (by columns)
typedef vector< vector > Matrix; Matrix matrix_sum(const Matrix& a, const Matrix& b) { // Pre: a and b are non-empty matrices // and are the same size // Post: returns a+b (sum of matrices) int nrows = a.size(); int ncols = a[0].size(); Matrix c(nrows, vector(ncols)); for (int j = 0; j < ncols; ++j) { for (int i = 0; i < nrows; ++i) { c[i][j] = a[i][j] + b[i][j]; } } return c; }
Transpose a matrix
void swap(int& a, int& b) { // Interchanges two values int c = a; a = b; b = c; } void Transpose(Matrix& m) { // Pre: m is a square matrix // Post: m contains the transpose of the input matrix int n = m.size(); for (int i = 0; i < n - 1; ++i) { for (int j = i + 1; j < n; ++j) { swap(m[i][j], m[j][i]); } } }
Is a matrix symmetric?
- (^) Design a procedure that indicates whether a matrix is symmetric: bool is_symmetric(const Matrix& m);
- (^) Observation: we only need to compare the upper with the lower triangular matrix.
symmetric not symmetric
Search in a matrix
- (^) Design a procedure that finds a value in a
matrix. If the value belongs to the matrix, the
procedure will return the location (i, j) at
which the value has been found.
void search(const Matrix& m, int x, int& i, int& j); // Pre: m is a non-empty matrix // Post: i and j define the location of a // cell that contains the value x in // m. In case x is not in m, then // i = j = -
Search in a matrix
void search(const Matrix& m, int x, int& i, int& j) { // Pre: m is a non-empty matrix // Post: i and j define the location of a cell that // contains the value x in M. In case x is not in m, // then i = j = - int nrows = m.size(); int ncols = m[0].size(); bool trobat = false; i = 0; while (i < nrows and not trobat) { j = 0; while (j < ncols and not trobat) { if (m[i][j] == x) trobat = true; else ++j; } if (not trobat) ++i; } if (not trobat){ i = -1; j = -1; } }
Search in a sorted matrix
- Example: let us find 10 in the matrix. We look at the lower left corner of the matrix.
- Since 13 > 10, the value cannot be found in the last row. 1 4 5 7 10 12 2 5 8 9 10 13 6 7 10 11 12 15 9 11 13 14 17 20 11 12 19 20 21 23 13 14 20 22 25 26
Search in a sorted matrix
- We look again at the lower left corner of the remaining matrix.
- Since 11 > 10, the value cannot be found in the row. 1 4 5 7 10 12 2 5 8 9 10 13 6 7 10 11 12 15 9 11 13 14 17 20 11 12 19 20 21 23 13 14 20 22 25 26
Search in a sorted matrix
- Since 11 > 10, the value cannot be found in the row. 1 4 5 7 10 12 2 5 8 9 10 13 6 7 10 11 12 15 9 11 13 14 17 20 11 12 19 20 21 23 13 14 20 22 25 26
Search in a sorted matrix
- Since 7 < 10, the value cannot be found in the column. 1 4 5 7 10 12 2 5 8 9 10 13 6 7 10 11 12 15 9 11 13 14 17 20 11 12 19 20 21 23 13 14 20 22 25 26