Changes for my application (Just Ignore)

Committer:
lucyannofrota
Date:
Wed Jun 02 20:48:17 2021 +0000
Revision:
6:1ade15e369fb
Parent:
5:a4014ab0a8cf
Temp kalm

Who changed what in which revision?

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