Matrix Library. v1.6.4 + some changes
Matrix.cpp@4:c0c8f3edd60e, 2011-10-22 (annotated)
- Committer:
- Yo_Robot
- Date:
- Sat Oct 22 23:19:51 2011 +0000
- Revision:
- 4:c0c8f3edd60e
- Parent:
- 3:589fb80932b5
- Child:
- 5:a4014ab0a8cf
version 1.6.2 Read Log.c for details
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 2:493402568a5e | 1 | /** |
Yo_Robot | 3:589fb80932b5 | 2 | * @file Matrix.cpp |
Yo_Robot | 3:589fb80932b5 | 3 | * @author Ernesto Palacios |
Yo_Robot | 3:589fb80932b5 | 4 | * @brief Source Code for the Matrix Class. |
Yo_Robot | 2:493402568a5e | 5 | * |
Yo_Robot | 2:493402568a5e | 6 | * Created on September 2011. |
Yo_Robot | 2:493402568a5e | 7 | * |
Yo_Robot | 2:493402568a5e | 8 | */ |
Yo_Robot | 2:493402568a5e | 9 | |
Yo_Robot | 0:3abd8c2d7c34 | 10 | #include "mbed.h" |
Yo_Robot | 0:3abd8c2d7c34 | 11 | #include "Matrix.h" |
Yo_Robot | 0:3abd8c2d7c34 | 12 | |
Yo_Robot | 1:48f417da268e | 13 | /// Rows by Cols Matrix Constructor |
Yo_Robot | 2:493402568a5e | 14 | Matrix::Matrix(int Rows, int Cols): _nRows(Rows), _nCols(Cols) |
Yo_Robot | 0:3abd8c2d7c34 | 15 | { |
Yo_Robot | 1:48f417da268e | 16 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 17 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 18 | _matrix[i].resize(_nCols); |
Yo_Robot | 2:493402568a5e | 19 | |
Yo_Robot | 2:493402568a5e | 20 | _pRow = 0; |
Yo_Robot | 2:493402568a5e | 21 | _pCol = 0; |
Yo_Robot | 2:493402568a5e | 22 | |
Yo_Robot | 2:493402568a5e | 23 | this->Clear(); //Make all elements zero by default. |
Yo_Robot | 0:3abd8c2d7c34 | 24 | } |
Yo_Robot | 0:3abd8c2d7c34 | 25 | |
Yo_Robot | 0:3abd8c2d7c34 | 26 | |
Yo_Robot | 2:493402568a5e | 27 | /// Copies one matrix into a new one |
Yo_Robot | 2:493402568a5e | 28 | Matrix::Matrix(const Matrix& base) |
Yo_Robot | 0:3abd8c2d7c34 | 29 | { |
Yo_Robot | 2:493402568a5e | 30 | _nCols = base._nCols; |
Yo_Robot | 2:493402568a5e | 31 | _nRows = base._nRows; |
Yo_Robot | 2:493402568a5e | 32 | |
Yo_Robot | 2:493402568a5e | 33 | _pRow = base._pRow; |
Yo_Robot | 2:493402568a5e | 34 | _pCol = base._pCol; |
Yo_Robot | 2:493402568a5e | 35 | |
Yo_Robot | 1:48f417da268e | 36 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 37 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 38 | _matrix[i].resize(_nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 39 | |
Yo_Robot | 1:48f417da268e | 40 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 41 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 42 | _matrix[i][j] = base._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 43 | } |
Yo_Robot | 0:3abd8c2d7c34 | 44 | |
Yo_Robot | 0:3abd8c2d7c34 | 45 | |
Yo_Robot | 1:48f417da268e | 46 | /// Default Constructor |
Yo_Robot | 2:493402568a5e | 47 | Matrix::Matrix() |
Yo_Robot | 0:3abd8c2d7c34 | 48 | { |
Yo_Robot | 2:493402568a5e | 49 | _nCols = 0; |
Yo_Robot | 2:493402568a5e | 50 | _nRows = 0; |
Yo_Robot | 1:48f417da268e | 51 | |
Yo_Robot | 2:493402568a5e | 52 | _pRow = 0; |
Yo_Robot | 2:493402568a5e | 53 | _pCol = 0; |
Yo_Robot | 4:c0c8f3edd60e | 54 | |
Yo_Robot | 0:3abd8c2d7c34 | 55 | } |
Yo_Robot | 0:3abd8c2d7c34 | 56 | |
Yo_Robot | 2:493402568a5e | 57 | /***********************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 58 | |
Yo_Robot | 1:48f417da268e | 59 | /// Returns true if matrix is full of zeros |
Yo_Robot | 1:48f417da268e | 60 | bool Matrix::isZero() |
Yo_Robot | 0:3abd8c2d7c34 | 61 | { |
Yo_Robot | 1:48f417da268e | 62 | bool zero = false; |
Yo_Robot | 1:48f417da268e | 63 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 64 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 65 | if( _matrix[i][j] != 0 ) |
Yo_Robot | 1:48f417da268e | 66 | zero = zero || true; |
Yo_Robot | 1:48f417da268e | 67 | return !zero; |
Yo_Robot | 0:3abd8c2d7c34 | 68 | } |
Yo_Robot | 0:3abd8c2d7c34 | 69 | |
Yo_Robot | 0:3abd8c2d7c34 | 70 | |
Yo_Robot | 1:48f417da268e | 71 | /// Returns true if Matrix is Single Row ot Single Column. |
Yo_Robot | 1:48f417da268e | 72 | bool Matrix::isVector() |
Yo_Robot | 0:3abd8c2d7c34 | 73 | { |
Yo_Robot | 2:493402568a5e | 74 | if( _nRows == 1 || _nCols == 1 ) |
Yo_Robot | 1:48f417da268e | 75 | return true; |
Yo_Robot | 1:48f417da268e | 76 | else |
Yo_Robot | 1:48f417da268e | 77 | return false; |
Yo_Robot | 0:3abd8c2d7c34 | 78 | } |
Yo_Robot | 0:3abd8c2d7c34 | 79 | |
Yo_Robot | 1:48f417da268e | 80 | /*************************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 81 | |
Yo_Robot | 1:48f417da268e | 82 | /// Returns all elements in Matrix as a single Row vector. |
Yo_Robot | 2:493402568a5e | 83 | const Matrix Matrix::ToPackedVector( const Matrix& Mat ) |
Yo_Robot | 0:3abd8c2d7c34 | 84 | { |
Yo_Robot | 4:c0c8f3edd60e | 85 | |
Yo_Robot | 2:493402568a5e | 86 | Matrix Crushed( 1, Mat._nRows * Mat._nCols ); |
Yo_Robot | 0:3abd8c2d7c34 | 87 | |
Yo_Robot | 0:3abd8c2d7c34 | 88 | int cont = 0; |
Yo_Robot | 2:493402568a5e | 89 | |
Yo_Robot | 2:493402568a5e | 90 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 91 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 92 | { |
Yo_Robot | 2:493402568a5e | 93 | Crushed._matrix[0][cont] = Mat._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 94 | cont++; |
Yo_Robot | 0:3abd8c2d7c34 | 95 | } |
Yo_Robot | 0:3abd8c2d7c34 | 96 | |
Yo_Robot | 2:493402568a5e | 97 | Crushed._pRow = Crushed._nRows; |
Yo_Robot | 2:493402568a5e | 98 | Crushed._pCol = Crushed._nCols; |
Yo_Robot | 2:493402568a5e | 99 | |
Yo_Robot | 2:493402568a5e | 100 | return Crushed; |
Yo_Robot | 0:3abd8c2d7c34 | 101 | } |
Yo_Robot | 0:3abd8c2d7c34 | 102 | |
Yo_Robot | 2:493402568a5e | 103 | |
Yo_Robot | 0:3abd8c2d7c34 | 104 | |
Yo_Robot | 2:493402568a5e | 105 | /// To add (Insert) a Single Row to a Matrix. |
Yo_Robot | 4:c0c8f3edd60e | 106 | void Matrix::AddRow(Matrix& Mat, int index) |
Yo_Robot | 0:3abd8c2d7c34 | 107 | { |
Yo_Robot | 4:c0c8f3edd60e | 108 | --index; |
Yo_Robot | 0:3abd8c2d7c34 | 109 | |
Yo_Robot | 4:c0c8f3edd60e | 110 | if( index > Mat._nRows + 1) |
Yo_Robot | 0:3abd8c2d7c34 | 111 | { |
Yo_Robot | 1:48f417da268e | 112 | printf("\n\nERROR:\nRow out of Limits @ AddRow()\n"); |
Yo_Robot | 2:493402568a5e | 113 | |
Yo_Robot | 0:3abd8c2d7c34 | 114 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 115 | |
Yo_Robot | 1:48f417da268e | 116 | Mat._nRows++; |
Yo_Robot | 1:48f417da268e | 117 | Mat._matrix.resize( Mat._nRows ); |
Yo_Robot | 1:48f417da268e | 118 | |
Yo_Robot | 1:48f417da268e | 119 | Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 120 | |
Yo_Robot | 4:c0c8f3edd60e | 121 | for( int i = Mat._nRows - 1; i > index; i-- ) |
Yo_Robot | 1:48f417da268e | 122 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 123 | Mat._matrix[i][j] = Mat._matrix[i - 1][j]; |
Yo_Robot | 1:48f417da268e | 124 | |
Yo_Robot | 1:48f417da268e | 125 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 4:c0c8f3edd60e | 126 | Mat._matrix[index][j] = 0.0; |
Yo_Robot | 0:3abd8c2d7c34 | 127 | } |
Yo_Robot | 0:3abd8c2d7c34 | 128 | } |
Yo_Robot | 0:3abd8c2d7c34 | 129 | |
Yo_Robot | 0:3abd8c2d7c34 | 130 | |
Yo_Robot | 4:c0c8f3edd60e | 131 | void Matrix::AddRow(Matrix& Receip, const Matrix& Row, int index) |
Yo_Robot | 4:c0c8f3edd60e | 132 | { |
Yo_Robot | 4:c0c8f3edd60e | 133 | Matrix::AddRow( Receip, index ); //Make Room |
Yo_Robot | 4:c0c8f3edd60e | 134 | |
Yo_Robot | 4:c0c8f3edd60e | 135 | --index; |
Yo_Robot | 4:c0c8f3edd60e | 136 | for( int i; i < Receip._nCols; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 137 | Receip._matrix[index][i] = Row._matrix[0][i]; //Copy Data. |
Yo_Robot | 4:c0c8f3edd60e | 138 | |
Yo_Robot | 4:c0c8f3edd60e | 139 | } |
Yo_Robot | 4:c0c8f3edd60e | 140 | |
Yo_Robot | 4:c0c8f3edd60e | 141 | |
Yo_Robot | 1:48f417da268e | 142 | /// To add (Insert) a single Column to a Matrix |
Yo_Robot | 4:c0c8f3edd60e | 143 | void Matrix::AddCol( Matrix& Mat, int index ) |
Yo_Robot | 1:48f417da268e | 144 | { |
Yo_Robot | 4:c0c8f3edd60e | 145 | --index; |
Yo_Robot | 0:3abd8c2d7c34 | 146 | |
Yo_Robot | 4:c0c8f3edd60e | 147 | if( index > Mat._nCols + 1 ) |
Yo_Robot | 1:48f417da268e | 148 | { |
Yo_Robot | 1:48f417da268e | 149 | printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); |
Yo_Robot | 2:493402568a5e | 150 | |
Yo_Robot | 1:48f417da268e | 151 | }else{ |
Yo_Robot | 1:48f417da268e | 152 | |
Yo_Robot | 1:48f417da268e | 153 | |
Yo_Robot | 1:48f417da268e | 154 | Mat._nCols++; |
Yo_Robot | 1:48f417da268e | 155 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 156 | Mat._matrix[i].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 157 | |
Yo_Robot | 1:48f417da268e | 158 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 159 | for( int j = Mat._nCols; j > index; j-- ) |
Yo_Robot | 1:48f417da268e | 160 | Mat._matrix[i][j] = Mat._matrix[i][j - 1]; |
Yo_Robot | 1:48f417da268e | 161 | |
Yo_Robot | 1:48f417da268e | 162 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 163 | Mat._matrix[i][index] = 0.0; |
Yo_Robot | 1:48f417da268e | 164 | |
Yo_Robot | 1:48f417da268e | 165 | } |
Yo_Robot | 1:48f417da268e | 166 | } |
Yo_Robot | 1:48f417da268e | 167 | |
Yo_Robot | 1:48f417da268e | 168 | |
Yo_Robot | 4:c0c8f3edd60e | 169 | void Matrix::AddCol(Matrix& Receip, const Matrix& Row, int index) |
Yo_Robot | 4:c0c8f3edd60e | 170 | { |
Yo_Robot | 4:c0c8f3edd60e | 171 | Matrix::AddCol( Receip, index ); // Make Rom |
Yo_Robot | 4:c0c8f3edd60e | 172 | |
Yo_Robot | 4:c0c8f3edd60e | 173 | --index; |
Yo_Robot | 4:c0c8f3edd60e | 174 | for( int i = 0; i < Receip._nRows; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 175 | Receip._matrix[i][index] = Row._matrix[i][0]; //Copy Data. |
Yo_Robot | 4:c0c8f3edd60e | 176 | } |
Yo_Robot | 4:c0c8f3edd60e | 177 | |
Yo_Robot | 4:c0c8f3edd60e | 178 | |
Yo_Robot | 1:48f417da268e | 179 | /// Delete a Single Column From Matrix. |
Yo_Robot | 2:493402568a5e | 180 | void Matrix::DeleteCol( Matrix& Mat, int Col) |
Yo_Robot | 0:3abd8c2d7c34 | 181 | { |
Yo_Robot | 0:3abd8c2d7c34 | 182 | --Col; // Because of Column zero. |
Yo_Robot | 0:3abd8c2d7c34 | 183 | |
Yo_Robot | 1:48f417da268e | 184 | if( Col > Mat._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 185 | { |
Yo_Robot | 1:48f417da268e | 186 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 2:493402568a5e | 187 | |
Yo_Robot | 0:3abd8c2d7c34 | 188 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 189 | |
Yo_Robot | 1:48f417da268e | 190 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 191 | for( int j = Col; j < Mat._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 192 | Mat._matrix[i][j] = Mat._matrix[i][j+1]; |
Yo_Robot | 0:3abd8c2d7c34 | 193 | |
Yo_Robot | 2:493402568a5e | 194 | // If adressing last element of Column, |
Yo_Robot | 2:493402568a5e | 195 | // wich no longer exists |
Yo_Robot | 2:493402568a5e | 196 | if( Mat._pCol == Mat._nCols ) |
Yo_Robot | 2:493402568a5e | 197 | Mat._pCol--; |
Yo_Robot | 2:493402568a5e | 198 | |
Yo_Robot | 0:3abd8c2d7c34 | 199 | // Decrease one column |
Yo_Robot | 1:48f417da268e | 200 | Mat._nCols--; |
Yo_Robot | 0:3abd8c2d7c34 | 201 | |
Yo_Robot | 0:3abd8c2d7c34 | 202 | //Erase last Column |
Yo_Robot | 1:48f417da268e | 203 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 204 | Mat._matrix[i].reserve(Mat._nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 205 | |
Yo_Robot | 0:3abd8c2d7c34 | 206 | } |
Yo_Robot | 0:3abd8c2d7c34 | 207 | } |
Yo_Robot | 0:3abd8c2d7c34 | 208 | |
Yo_Robot | 1:48f417da268e | 209 | |
Yo_Robot | 1:48f417da268e | 210 | /// Delete a Single Row form Matrix |
Yo_Robot | 1:48f417da268e | 211 | void Matrix::DeleteRow(Matrix& Mat, int Row) |
Yo_Robot | 1:48f417da268e | 212 | { |
Yo_Robot | 1:48f417da268e | 213 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 214 | |
Yo_Robot | 1:48f417da268e | 215 | if( Row > Mat._nRows ) |
Yo_Robot | 1:48f417da268e | 216 | { |
Yo_Robot | 1:48f417da268e | 217 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 2:493402568a5e | 218 | |
Yo_Robot | 1:48f417da268e | 219 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 220 | |
Yo_Robot | 1:48f417da268e | 221 | for( int i = Row; i < Mat._nRows - 1; i++ ) |
Yo_Robot | 2:493402568a5e | 222 | |
Yo_Robot | 1:48f417da268e | 223 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 224 | Mat._matrix[i][j] = Mat._matrix[i+1][j]; |
Yo_Robot | 1:48f417da268e | 225 | Mat._nRows--; |
Yo_Robot | 1:48f417da268e | 226 | Mat._matrix.resize(Mat._nRows); |
Yo_Robot | 1:48f417da268e | 227 | } |
Yo_Robot | 1:48f417da268e | 228 | } |
Yo_Robot | 1:48f417da268e | 229 | |
Yo_Robot | 1:48f417da268e | 230 | /*****************************************************************************************/ |
Yo_Robot | 1:48f417da268e | 231 | |
Yo_Robot | 1:48f417da268e | 232 | /// Extracts a single row form calling matrix and saves it to another matrix. |
Yo_Robot | 2:493402568a5e | 233 | const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) |
Yo_Robot | 0:3abd8c2d7c34 | 234 | { |
Yo_Robot | 2:493402568a5e | 235 | --row; |
Yo_Robot | 1:48f417da268e | 236 | |
Yo_Robot | 2:493402568a5e | 237 | if( row > Mat._nRows ) |
Yo_Robot | 0:3abd8c2d7c34 | 238 | { |
Yo_Robot | 2:493402568a5e | 239 | printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" |
Yo_Robot | 1:48f417da268e | 240 | "Nothing Done.\n\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 241 | |
Yo_Robot | 0:3abd8c2d7c34 | 242 | }else{ |
Yo_Robot | 1:48f417da268e | 243 | |
Yo_Robot | 2:493402568a5e | 244 | Matrix SingleRow( 1 , Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 245 | SingleRow.Clear(); |
Yo_Robot | 4:c0c8f3edd60e | 246 | |
Yo_Robot | 2:493402568a5e | 247 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 248 | SingleRow._matrix[0][j] = Mat._matrix[row][j]; |
Yo_Robot | 2:493402568a5e | 249 | |
Yo_Robot | 2:493402568a5e | 250 | SingleRow._pCol = SingleRow._nCols; |
Yo_Robot | 2:493402568a5e | 251 | SingleRow._pRow = 0; |
Yo_Robot | 2:493402568a5e | 252 | |
Yo_Robot | 2:493402568a5e | 253 | return SingleRow; |
Yo_Robot | 0:3abd8c2d7c34 | 254 | } |
Yo_Robot | 0:3abd8c2d7c34 | 255 | } |
Yo_Robot | 0:3abd8c2d7c34 | 256 | |
Yo_Robot | 0:3abd8c2d7c34 | 257 | |
Yo_Robot | 1:48f417da268e | 258 | /// Extracts a single column form calling matrix and saves it to another matrix. |
Yo_Robot | 2:493402568a5e | 259 | const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) |
Yo_Robot | 1:48f417da268e | 260 | { |
Yo_Robot | 2:493402568a5e | 261 | --col; |
Yo_Robot | 4:c0c8f3edd60e | 262 | |
Yo_Robot | 2:493402568a5e | 263 | if( col > Mat._nCols ) |
Yo_Robot | 1:48f417da268e | 264 | { |
Yo_Robot | 2:493402568a5e | 265 | printf( "\n\nERROR:\nColumn out of dimmensions.\n" |
Yo_Robot | 1:48f417da268e | 266 | "Nothing Done.\n\n" ); |
Yo_Robot | 1:48f417da268e | 267 | }else{ |
Yo_Robot | 1:48f417da268e | 268 | |
Yo_Robot | 2:493402568a5e | 269 | Matrix SingleCol( Mat._nRows, 1 ); |
Yo_Robot | 2:493402568a5e | 270 | for(int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 271 | SingleCol._matrix[i][0] = Mat._matrix[i][col]; |
Yo_Robot | 4:c0c8f3edd60e | 272 | |
Yo_Robot | 2:493402568a5e | 273 | SingleCol._pCol = 0; |
Yo_Robot | 2:493402568a5e | 274 | SingleCol._pRow = SingleCol._nRows; |
Yo_Robot | 4:c0c8f3edd60e | 275 | |
Yo_Robot | 2:493402568a5e | 276 | return SingleCol; |
Yo_Robot | 1:48f417da268e | 277 | } |
Yo_Robot | 1:48f417da268e | 278 | } |
Yo_Robot | 1:48f417da268e | 279 | |
Yo_Robot | 1:48f417da268e | 280 | |
Yo_Robot | 1:48f417da268e | 281 | /// Makes matrix Bigger! |
Yo_Robot | 1:48f417da268e | 282 | void Matrix::Resize( int Rows, int Cols ) |
Yo_Robot | 1:48f417da268e | 283 | { |
Yo_Robot | 2:493402568a5e | 284 | _nRows = Rows; //Decreases one because internally |
Yo_Robot | 2:493402568a5e | 285 | _nCols = Cols; // Index starts at zero. |
Yo_Robot | 2:493402568a5e | 286 | |
Yo_Robot | 2:493402568a5e | 287 | _matrix.resize( _nRows ); |
Yo_Robot | 2:493402568a5e | 288 | |
Yo_Robot | 1:48f417da268e | 289 | for( int i = 0; i< _nRows ; i++ ) |
Yo_Robot | 1:48f417da268e | 290 | _matrix[i].resize(_nCols); |
Yo_Robot | 2:493402568a5e | 291 | |
Yo_Robot | 2:493402568a5e | 292 | _pRow = 0; // If matrix is resized the << |
Yo_Robot | 2:493402568a5e | 293 | _pCol = 0; // operator overwrites everything! |
Yo_Robot | 1:48f417da268e | 294 | } |
Yo_Robot | 1:48f417da268e | 295 | |
Yo_Robot | 1:48f417da268e | 296 | |
Yo_Robot | 1:48f417da268e | 297 | /// Ask user for elemnts in Matrix |
Yo_Robot | 0:3abd8c2d7c34 | 298 | void Matrix::FillMatrix() |
Yo_Robot | 0:3abd8c2d7c34 | 299 | { |
Yo_Robot | 1:48f417da268e | 300 | for(int i = 0; i < _nRows; i++) |
Yo_Robot | 0:3abd8c2d7c34 | 301 | { |
Yo_Robot | 1:48f417da268e | 302 | for(int j = 0; j < _nCols; j++) |
Yo_Robot | 0:3abd8c2d7c34 | 303 | { |
Yo_Robot | 0:3abd8c2d7c34 | 304 | printf( "Position [%u][%u]: ", i, j ); |
Yo_Robot | 0:3abd8c2d7c34 | 305 | float numero; |
Yo_Robot | 0:3abd8c2d7c34 | 306 | scanf( "%f", &numero ); |
Yo_Robot | 0:3abd8c2d7c34 | 307 | printf("%.3f ", numero); |
Yo_Robot | 1:48f417da268e | 308 | this->_matrix[i][j] = numero; |
Yo_Robot | 0:3abd8c2d7c34 | 309 | } |
Yo_Robot | 0:3abd8c2d7c34 | 310 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 311 | } |
Yo_Robot | 0:3abd8c2d7c34 | 312 | printf("\n"); |
Yo_Robot | 2:493402568a5e | 313 | |
Yo_Robot | 2:493402568a5e | 314 | _pRow = _nRows; |
Yo_Robot | 2:493402568a5e | 315 | _pCol = _nCols; |
Yo_Robot | 0:3abd8c2d7c34 | 316 | } |
Yo_Robot | 0:3abd8c2d7c34 | 317 | |
Yo_Robot | 0:3abd8c2d7c34 | 318 | |
Yo_Robot | 1:48f417da268e | 319 | /// Prints out Matrix. |
Yo_Robot | 0:3abd8c2d7c34 | 320 | void Matrix::print() |
Yo_Robot | 0:3abd8c2d7c34 | 321 | { |
Yo_Robot | 1:48f417da268e | 322 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 323 | { |
Yo_Robot | 1:48f417da268e | 324 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 325 | { |
Yo_Robot | 0:3abd8c2d7c34 | 326 | printf( "%.3f, ",_matrix[i][j] ); |
Yo_Robot | 1:48f417da268e | 327 | |
Yo_Robot | 0:3abd8c2d7c34 | 328 | } |
Yo_Robot | 1:48f417da268e | 329 | printf( "\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 330 | } |
Yo_Robot | 0:3abd8c2d7c34 | 331 | } |
Yo_Robot | 0:3abd8c2d7c34 | 332 | |
Yo_Robot | 0:3abd8c2d7c34 | 333 | |
Yo_Robot | 1:48f417da268e | 334 | /// Fills matrix with zeros. |
Yo_Robot | 1:48f417da268e | 335 | void Matrix::Clear() |
Yo_Robot | 1:48f417da268e | 336 | { |
Yo_Robot | 1:48f417da268e | 337 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 338 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 339 | _matrix[i][j] = 0; |
Yo_Robot | 2:493402568a5e | 340 | |
Yo_Robot | 2:493402568a5e | 341 | _pCol = 0; // New data can be added |
Yo_Robot | 2:493402568a5e | 342 | _pRow = 0; |
Yo_Robot | 1:48f417da268e | 343 | } |
Yo_Robot | 0:3abd8c2d7c34 | 344 | |
Yo_Robot | 1:48f417da268e | 345 | /********************************************************************************/ |
Yo_Robot | 1:48f417da268e | 346 | |
Yo_Robot | 2:493402568a5e | 347 | |
Yo_Robot | 1:48f417da268e | 348 | /// Inserts a Single element in a desired Position( Index starts at [1][1] ); |
Yo_Robot | 1:48f417da268e | 349 | void Matrix::add(int Row, int Col, float number) |
Yo_Robot | 1:48f417da268e | 350 | { |
Yo_Robot | 1:48f417da268e | 351 | --Col; --Row; |
Yo_Robot | 2:493402568a5e | 352 | |
Yo_Robot | 1:48f417da268e | 353 | if( Row > _nRows || Col > _nCols ) |
Yo_Robot | 1:48f417da268e | 354 | { |
Yo_Robot | 1:48f417da268e | 355 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); |
Yo_Robot | 2:493402568a5e | 356 | |
Yo_Robot | 1:48f417da268e | 357 | }else{ |
Yo_Robot | 1:48f417da268e | 358 | _matrix[Row][Col] = number; |
Yo_Robot | 1:48f417da268e | 359 | } |
Yo_Robot | 1:48f417da268e | 360 | } |
Yo_Robot | 1:48f417da268e | 361 | |
Yo_Robot | 1:48f417da268e | 362 | |
Yo_Robot | 1:48f417da268e | 363 | /// Adds all elements in matrix and returns the answer. |
Yo_Robot | 1:48f417da268e | 364 | float Matrix::sum() |
Yo_Robot | 0:3abd8c2d7c34 | 365 | { |
Yo_Robot | 0:3abd8c2d7c34 | 366 | float total; |
Yo_Robot | 0:3abd8c2d7c34 | 367 | |
Yo_Robot | 1:48f417da268e | 368 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 369 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 370 | total += this->_matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 371 | return total; |
Yo_Robot | 0:3abd8c2d7c34 | 372 | } |
Yo_Robot | 0:3abd8c2d7c34 | 373 | |
Yo_Robot | 0:3abd8c2d7c34 | 374 | |
Yo_Robot | 1:48f417da268e | 375 | /// Returns the specified element. Index Starts at [1][1]. |
Yo_Robot | 1:48f417da268e | 376 | float Matrix::getNumber( int Row, int Col ) |
Yo_Robot | 1:48f417da268e | 377 | { return this->_matrix[Row -1][Col - 1]; } |
Yo_Robot | 0:3abd8c2d7c34 | 378 | |
Yo_Robot | 1:48f417da268e | 379 | /// Returns the number of Rows in Matrix. |
Yo_Robot | 1:48f417da268e | 380 | int Matrix::getRows(){ return this->_nRows; } |
Yo_Robot | 0:3abd8c2d7c34 | 381 | |
Yo_Robot | 0:3abd8c2d7c34 | 382 | |
Yo_Robot | 1:48f417da268e | 383 | /// Returns the number of Columns in Matrix. |
Yo_Robot | 2:493402568a5e | 384 | int Matrix::getCols(){ return this->_nCols; } |