Matrix Library. v1.6.4 + some changes

Committer:
Yo_Robot
Date:
Sun Oct 30 16:29:23 2011 +0000
Revision:
5:a4014ab0a8cf
Parent:
4:c0c8f3edd60e
Child:
6:0c77fd691ba8
Version 1.6.4  View Log.c for changes.

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