use from Ernesto Palacios
Dependents: RoboticArm DXL_SDK_Porting_Test
Fork of Matrix by
Matrix.cpp@3:589fb80932b5, 2011-10-21 (annotated)
- Committer:
- Yo_Robot
- Date:
- Fri Oct 21 03:33:53 2011 +0000
- Revision:
- 3:589fb80932b5
- Parent:
- 2:493402568a5e
- Child:
- 4:c0c8f3edd60e
version 1.6.0.1 just fixed some typos in comments
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 | 2:493402568a5e | 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 | 2:493402568a5e | 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 | 1:48f417da268e | 106 | void Matrix::AddRow(Matrix& Mat, int Row) |
Yo_Robot | 0:3abd8c2d7c34 | 107 | { |
Yo_Robot | 0:3abd8c2d7c34 | 108 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 109 | |
Yo_Robot | 1:48f417da268e | 110 | if( Row > 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 | 1:48f417da268e | 121 | for( int i = Mat._nRows - 1; i > Row; 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 | 1:48f417da268e | 126 | Mat._matrix[Row][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 | 1:48f417da268e | 131 | /// To add (Insert) a single Column to a Matrix |
Yo_Robot | 2:493402568a5e | 132 | void Matrix::AddColumn( Matrix& Mat, int Col ) |
Yo_Robot | 1:48f417da268e | 133 | { |
Yo_Robot | 1:48f417da268e | 134 | --Col; |
Yo_Robot | 0:3abd8c2d7c34 | 135 | |
Yo_Robot | 1:48f417da268e | 136 | if( Col > Mat._nCols + 1 ) |
Yo_Robot | 1:48f417da268e | 137 | { |
Yo_Robot | 1:48f417da268e | 138 | printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); |
Yo_Robot | 2:493402568a5e | 139 | |
Yo_Robot | 1:48f417da268e | 140 | }else{ |
Yo_Robot | 1:48f417da268e | 141 | |
Yo_Robot | 1:48f417da268e | 142 | |
Yo_Robot | 1:48f417da268e | 143 | Mat._nCols++; |
Yo_Robot | 1:48f417da268e | 144 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 145 | Mat._matrix[i].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 146 | |
Yo_Robot | 1:48f417da268e | 147 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 148 | for( int j = Mat._nCols; j > Col; j-- ) |
Yo_Robot | 1:48f417da268e | 149 | Mat._matrix[i][j] = Mat._matrix[i][j - 1]; |
Yo_Robot | 1:48f417da268e | 150 | |
Yo_Robot | 1:48f417da268e | 151 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 152 | Mat._matrix[i][Col] = 0.0; |
Yo_Robot | 1:48f417da268e | 153 | |
Yo_Robot | 1:48f417da268e | 154 | } |
Yo_Robot | 1:48f417da268e | 155 | } |
Yo_Robot | 1:48f417da268e | 156 | |
Yo_Robot | 1:48f417da268e | 157 | |
Yo_Robot | 1:48f417da268e | 158 | /// Delete a Single Column From Matrix. |
Yo_Robot | 2:493402568a5e | 159 | void Matrix::DeleteCol( Matrix& Mat, int Col) |
Yo_Robot | 0:3abd8c2d7c34 | 160 | { |
Yo_Robot | 0:3abd8c2d7c34 | 161 | --Col; // Because of Column zero. |
Yo_Robot | 0:3abd8c2d7c34 | 162 | |
Yo_Robot | 1:48f417da268e | 163 | if( Col > Mat._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 164 | { |
Yo_Robot | 1:48f417da268e | 165 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 2:493402568a5e | 166 | |
Yo_Robot | 0:3abd8c2d7c34 | 167 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 168 | |
Yo_Robot | 1:48f417da268e | 169 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 170 | for( int j = Col; j < Mat._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 171 | Mat._matrix[i][j] = Mat._matrix[i][j+1]; |
Yo_Robot | 0:3abd8c2d7c34 | 172 | |
Yo_Robot | 2:493402568a5e | 173 | // If adressing last element of Column, |
Yo_Robot | 2:493402568a5e | 174 | // wich no longer exists |
Yo_Robot | 2:493402568a5e | 175 | if( Mat._pCol == Mat._nCols ) |
Yo_Robot | 2:493402568a5e | 176 | Mat._pCol--; |
Yo_Robot | 2:493402568a5e | 177 | |
Yo_Robot | 0:3abd8c2d7c34 | 178 | // Decrease one column |
Yo_Robot | 1:48f417da268e | 179 | Mat._nCols--; |
Yo_Robot | 0:3abd8c2d7c34 | 180 | |
Yo_Robot | 0:3abd8c2d7c34 | 181 | //Erase last Column |
Yo_Robot | 1:48f417da268e | 182 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 183 | Mat._matrix[i].reserve(Mat._nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 184 | |
Yo_Robot | 0:3abd8c2d7c34 | 185 | } |
Yo_Robot | 0:3abd8c2d7c34 | 186 | } |
Yo_Robot | 0:3abd8c2d7c34 | 187 | |
Yo_Robot | 1:48f417da268e | 188 | |
Yo_Robot | 1:48f417da268e | 189 | /// Delete a Single Row form Matrix |
Yo_Robot | 1:48f417da268e | 190 | void Matrix::DeleteRow(Matrix& Mat, int Row) |
Yo_Robot | 1:48f417da268e | 191 | { |
Yo_Robot | 1:48f417da268e | 192 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 193 | |
Yo_Robot | 1:48f417da268e | 194 | if( Row > Mat._nRows ) |
Yo_Robot | 1:48f417da268e | 195 | { |
Yo_Robot | 1:48f417da268e | 196 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 2:493402568a5e | 197 | |
Yo_Robot | 1:48f417da268e | 198 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 199 | |
Yo_Robot | 1:48f417da268e | 200 | for( int i = Row; i < Mat._nRows - 1; i++ ) |
Yo_Robot | 2:493402568a5e | 201 | |
Yo_Robot | 1:48f417da268e | 202 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 203 | Mat._matrix[i][j] = Mat._matrix[i+1][j]; |
Yo_Robot | 1:48f417da268e | 204 | Mat._nRows--; |
Yo_Robot | 1:48f417da268e | 205 | Mat._matrix.resize(Mat._nRows); |
Yo_Robot | 1:48f417da268e | 206 | } |
Yo_Robot | 1:48f417da268e | 207 | } |
Yo_Robot | 1:48f417da268e | 208 | |
Yo_Robot | 1:48f417da268e | 209 | /*****************************************************************************************/ |
Yo_Robot | 1:48f417da268e | 210 | |
Yo_Robot | 1:48f417da268e | 211 | /// Extracts a single row form calling matrix and saves it to another matrix. |
Yo_Robot | 2:493402568a5e | 212 | const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) |
Yo_Robot | 0:3abd8c2d7c34 | 213 | { |
Yo_Robot | 2:493402568a5e | 214 | --row; |
Yo_Robot | 1:48f417da268e | 215 | |
Yo_Robot | 2:493402568a5e | 216 | if( row > Mat._nRows ) |
Yo_Robot | 0:3abd8c2d7c34 | 217 | { |
Yo_Robot | 2:493402568a5e | 218 | printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" |
Yo_Robot | 1:48f417da268e | 219 | "Nothing Done.\n\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 220 | |
Yo_Robot | 0:3abd8c2d7c34 | 221 | }else{ |
Yo_Robot | 1:48f417da268e | 222 | |
Yo_Robot | 2:493402568a5e | 223 | Matrix SingleRow( 1 , Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 224 | SingleRow.Clear(); |
Yo_Robot | 1:48f417da268e | 225 | |
Yo_Robot | 2:493402568a5e | 226 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 227 | SingleRow._matrix[0][j] = Mat._matrix[row][j]; |
Yo_Robot | 2:493402568a5e | 228 | |
Yo_Robot | 2:493402568a5e | 229 | SingleRow._pCol = SingleRow._nCols; |
Yo_Robot | 2:493402568a5e | 230 | SingleRow._pRow = 0; |
Yo_Robot | 2:493402568a5e | 231 | |
Yo_Robot | 2:493402568a5e | 232 | return SingleRow; |
Yo_Robot | 0:3abd8c2d7c34 | 233 | } |
Yo_Robot | 0:3abd8c2d7c34 | 234 | } |
Yo_Robot | 0:3abd8c2d7c34 | 235 | |
Yo_Robot | 0:3abd8c2d7c34 | 236 | |
Yo_Robot | 1:48f417da268e | 237 | /// Extracts a single column form calling matrix and saves it to another matrix. |
Yo_Robot | 2:493402568a5e | 238 | const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) |
Yo_Robot | 1:48f417da268e | 239 | { |
Yo_Robot | 2:493402568a5e | 240 | --col; |
Yo_Robot | 2:493402568a5e | 241 | |
Yo_Robot | 2:493402568a5e | 242 | if( col > Mat._nCols ) |
Yo_Robot | 1:48f417da268e | 243 | { |
Yo_Robot | 2:493402568a5e | 244 | printf( "\n\nERROR:\nColumn out of dimmensions.\n" |
Yo_Robot | 1:48f417da268e | 245 | "Nothing Done.\n\n" ); |
Yo_Robot | 1:48f417da268e | 246 | }else{ |
Yo_Robot | 1:48f417da268e | 247 | |
Yo_Robot | 2:493402568a5e | 248 | Matrix SingleCol( Mat._nRows, 1 ); |
Yo_Robot | 2:493402568a5e | 249 | for(int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 250 | SingleCol._matrix[i][0] = Mat._matrix[i][col]; |
Yo_Robot | 2:493402568a5e | 251 | |
Yo_Robot | 2:493402568a5e | 252 | SingleCol._pCol = 0; |
Yo_Robot | 2:493402568a5e | 253 | SingleCol._pRow = SingleCol._nRows; |
Yo_Robot | 2:493402568a5e | 254 | |
Yo_Robot | 2:493402568a5e | 255 | return SingleCol; |
Yo_Robot | 1:48f417da268e | 256 | } |
Yo_Robot | 1:48f417da268e | 257 | } |
Yo_Robot | 1:48f417da268e | 258 | |
Yo_Robot | 1:48f417da268e | 259 | |
Yo_Robot | 1:48f417da268e | 260 | /// Makes matrix Bigger! |
Yo_Robot | 1:48f417da268e | 261 | void Matrix::Resize( int Rows, int Cols ) |
Yo_Robot | 1:48f417da268e | 262 | { |
Yo_Robot | 2:493402568a5e | 263 | _nRows = Rows; //Decreases one because internally |
Yo_Robot | 2:493402568a5e | 264 | _nCols = Cols; // Index starts at zero. |
Yo_Robot | 2:493402568a5e | 265 | |
Yo_Robot | 2:493402568a5e | 266 | _matrix.resize( _nRows ); |
Yo_Robot | 2:493402568a5e | 267 | |
Yo_Robot | 1:48f417da268e | 268 | for( int i = 0; i< _nRows ; i++ ) |
Yo_Robot | 1:48f417da268e | 269 | _matrix[i].resize(_nCols); |
Yo_Robot | 2:493402568a5e | 270 | |
Yo_Robot | 2:493402568a5e | 271 | _pRow = 0; // If matrix is resized the << |
Yo_Robot | 2:493402568a5e | 272 | _pCol = 0; // operator overwrites everything! |
Yo_Robot | 1:48f417da268e | 273 | } |
Yo_Robot | 1:48f417da268e | 274 | |
Yo_Robot | 1:48f417da268e | 275 | |
Yo_Robot | 1:48f417da268e | 276 | /// Ask user for elemnts in Matrix |
Yo_Robot | 0:3abd8c2d7c34 | 277 | void Matrix::FillMatrix() |
Yo_Robot | 0:3abd8c2d7c34 | 278 | { |
Yo_Robot | 1:48f417da268e | 279 | for(int i = 0; i < _nRows; i++) |
Yo_Robot | 0:3abd8c2d7c34 | 280 | { |
Yo_Robot | 1:48f417da268e | 281 | for(int j = 0; j < _nCols; j++) |
Yo_Robot | 0:3abd8c2d7c34 | 282 | { |
Yo_Robot | 0:3abd8c2d7c34 | 283 | printf( "Position [%u][%u]: ", i, j ); |
Yo_Robot | 0:3abd8c2d7c34 | 284 | float numero; |
Yo_Robot | 0:3abd8c2d7c34 | 285 | scanf( "%f", &numero ); |
Yo_Robot | 0:3abd8c2d7c34 | 286 | printf("%.3f ", numero); |
Yo_Robot | 1:48f417da268e | 287 | this->_matrix[i][j] = numero; |
Yo_Robot | 0:3abd8c2d7c34 | 288 | } |
Yo_Robot | 0:3abd8c2d7c34 | 289 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 290 | } |
Yo_Robot | 0:3abd8c2d7c34 | 291 | printf("\n"); |
Yo_Robot | 2:493402568a5e | 292 | |
Yo_Robot | 2:493402568a5e | 293 | _pRow = _nRows; |
Yo_Robot | 2:493402568a5e | 294 | _pCol = _nCols; |
Yo_Robot | 0:3abd8c2d7c34 | 295 | } |
Yo_Robot | 0:3abd8c2d7c34 | 296 | |
Yo_Robot | 0:3abd8c2d7c34 | 297 | |
Yo_Robot | 1:48f417da268e | 298 | /// Prints out Matrix. |
Yo_Robot | 0:3abd8c2d7c34 | 299 | void Matrix::print() |
Yo_Robot | 0:3abd8c2d7c34 | 300 | { |
Yo_Robot | 1:48f417da268e | 301 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 302 | { |
Yo_Robot | 1:48f417da268e | 303 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 304 | { |
Yo_Robot | 0:3abd8c2d7c34 | 305 | printf( "%.3f, ",_matrix[i][j] ); |
Yo_Robot | 1:48f417da268e | 306 | |
Yo_Robot | 0:3abd8c2d7c34 | 307 | } |
Yo_Robot | 1:48f417da268e | 308 | printf( "\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 309 | } |
Yo_Robot | 0:3abd8c2d7c34 | 310 | } |
Yo_Robot | 0:3abd8c2d7c34 | 311 | |
Yo_Robot | 0:3abd8c2d7c34 | 312 | |
Yo_Robot | 1:48f417da268e | 313 | /// Fills matrix with zeros. |
Yo_Robot | 1:48f417da268e | 314 | void Matrix::Clear() |
Yo_Robot | 1:48f417da268e | 315 | { |
Yo_Robot | 1:48f417da268e | 316 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 317 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 318 | _matrix[i][j] = 0; |
Yo_Robot | 2:493402568a5e | 319 | |
Yo_Robot | 2:493402568a5e | 320 | _pCol = 0; // New data can be added |
Yo_Robot | 2:493402568a5e | 321 | _pRow = 0; |
Yo_Robot | 1:48f417da268e | 322 | } |
Yo_Robot | 0:3abd8c2d7c34 | 323 | |
Yo_Robot | 1:48f417da268e | 324 | /********************************************************************************/ |
Yo_Robot | 1:48f417da268e | 325 | |
Yo_Robot | 2:493402568a5e | 326 | |
Yo_Robot | 1:48f417da268e | 327 | /// Inserts a Single element in a desired Position( Index starts at [1][1] ); |
Yo_Robot | 1:48f417da268e | 328 | void Matrix::add(int Row, int Col, float number) |
Yo_Robot | 1:48f417da268e | 329 | { |
Yo_Robot | 1:48f417da268e | 330 | --Col; --Row; |
Yo_Robot | 2:493402568a5e | 331 | |
Yo_Robot | 1:48f417da268e | 332 | if( Row > _nRows || Col > _nCols ) |
Yo_Robot | 1:48f417da268e | 333 | { |
Yo_Robot | 1:48f417da268e | 334 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); |
Yo_Robot | 2:493402568a5e | 335 | |
Yo_Robot | 1:48f417da268e | 336 | }else{ |
Yo_Robot | 1:48f417da268e | 337 | _matrix[Row][Col] = number; |
Yo_Robot | 1:48f417da268e | 338 | } |
Yo_Robot | 1:48f417da268e | 339 | } |
Yo_Robot | 1:48f417da268e | 340 | |
Yo_Robot | 1:48f417da268e | 341 | |
Yo_Robot | 1:48f417da268e | 342 | /// Adds all elements in matrix and returns the answer. |
Yo_Robot | 1:48f417da268e | 343 | float Matrix::sum() |
Yo_Robot | 0:3abd8c2d7c34 | 344 | { |
Yo_Robot | 0:3abd8c2d7c34 | 345 | float total; |
Yo_Robot | 0:3abd8c2d7c34 | 346 | |
Yo_Robot | 1:48f417da268e | 347 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 348 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 349 | total += this->_matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 350 | return total; |
Yo_Robot | 0:3abd8c2d7c34 | 351 | } |
Yo_Robot | 0:3abd8c2d7c34 | 352 | |
Yo_Robot | 0:3abd8c2d7c34 | 353 | |
Yo_Robot | 1:48f417da268e | 354 | /// Returns the specified element. Index Starts at [1][1]. |
Yo_Robot | 1:48f417da268e | 355 | float Matrix::getNumber( int Row, int Col ) |
Yo_Robot | 1:48f417da268e | 356 | { return this->_matrix[Row -1][Col - 1]; } |
Yo_Robot | 0:3abd8c2d7c34 | 357 | |
Yo_Robot | 1:48f417da268e | 358 | /// Returns the number of Rows in Matrix. |
Yo_Robot | 1:48f417da268e | 359 | int Matrix::getRows(){ return this->_nRows; } |
Yo_Robot | 0:3abd8c2d7c34 | 360 | |
Yo_Robot | 0:3abd8c2d7c34 | 361 | |
Yo_Robot | 1:48f417da268e | 362 | /// Returns the number of Columns in Matrix. |
Yo_Robot | 2:493402568a5e | 363 | int Matrix::getCols(){ return this->_nCols; } |