Matrix Library. v1.6.4

Dependents:   Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more

Committer:
Yo_Robot
Date:
Thu Oct 20 23:42:13 2011 +0000
Revision:
2:493402568a5e
Parent:
1:48f417da268e
Child:
3:589fb80932b5
Everything working fine, dropped some useless methods, Matrix Class is Complete, maybe I\ll keep adding stuff but basic usage is covered in my opinion.

Who changed what in which revision?

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