Matrix Library. v1.6.4 + some changes

Committer:
saloutos
Date:
Thu Oct 01 04:06:38 2020 +0000
Revision:
6:0c77fd691ba8
Parent:
5:a4014ab0a8cf
Child:
7:444fdf9b7d4c
Compile-able copy of Matrix library...some functions may return values instead of errors, but errors will be printed to terminal

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
saloutos 6:0c77fd691ba8 240 Matrix SingleRow( 1 , Mat._nCols );
saloutos 6:0c77fd691ba8 241 SingleRow.Clear();
saloutos 6:0c77fd691ba8 242
Yo_Robot 2:493402568a5e 243 if( row > Mat._nRows )
Yo_Robot 0:3abd8c2d7c34 244 {
Yo_Robot 2:493402568a5e 245 printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n"
Yo_Robot 1:48f417da268e 246 "Nothing Done.\n\n" );
saloutos 6:0c77fd691ba8 247
saloutos 6:0c77fd691ba8 248 return SingleRow;
Yo_Robot 0:3abd8c2d7c34 249
Yo_Robot 0:3abd8c2d7c34 250 }else{
Yo_Robot 1:48f417da268e 251
Yo_Robot 2:493402568a5e 252 for( int j = 0; j < Mat._nCols; j++ )
Yo_Robot 5:a4014ab0a8cf 253 SingleRow._matrix[0][j] = Mat._matrix[row][j];
Yo_Robot 2:493402568a5e 254
Yo_Robot 2:493402568a5e 255 SingleRow._pCol = SingleRow._nCols;
Yo_Robot 2:493402568a5e 256 SingleRow._pRow = 0;
Yo_Robot 2:493402568a5e 257
Yo_Robot 2:493402568a5e 258 return SingleRow;
Yo_Robot 0:3abd8c2d7c34 259 }
Yo_Robot 0:3abd8c2d7c34 260 }
Yo_Robot 0:3abd8c2d7c34 261
Yo_Robot 0:3abd8c2d7c34 262
Yo_Robot 1:48f417da268e 263 /// Extracts a single column form calling matrix and saves it to another matrix.
Yo_Robot 2:493402568a5e 264 const Matrix Matrix::ExportCol( const Matrix& Mat, int col )
Yo_Robot 1:48f417da268e 265 {
Yo_Robot 2:493402568a5e 266 --col;
Yo_Robot 4:c0c8f3edd60e 267
saloutos 6:0c77fd691ba8 268 Matrix SingleCol( Mat._nRows, 1 );
saloutos 6:0c77fd691ba8 269
Yo_Robot 2:493402568a5e 270 if( col > Mat._nCols )
Yo_Robot 1:48f417da268e 271 {
Yo_Robot 2:493402568a5e 272 printf( "\n\nERROR:\nColumn out of dimmensions.\n"
Yo_Robot 1:48f417da268e 273 "Nothing Done.\n\n" );
Yo_Robot 1:48f417da268e 274 }else{
Yo_Robot 1:48f417da268e 275
saloutos 6:0c77fd691ba8 276
Yo_Robot 2:493402568a5e 277 for(int i = 0; i < Mat._nRows; i++ )
Yo_Robot 2:493402568a5e 278 SingleCol._matrix[i][0] = Mat._matrix[i][col];
Yo_Robot 4:c0c8f3edd60e 279
Yo_Robot 2:493402568a5e 280 SingleCol._pCol = 0;
Yo_Robot 2:493402568a5e 281 SingleCol._pRow = SingleCol._nRows;
Yo_Robot 4:c0c8f3edd60e 282
saloutos 6:0c77fd691ba8 283
Yo_Robot 1:48f417da268e 284 }
saloutos 6:0c77fd691ba8 285 return SingleCol;
Yo_Robot 1:48f417da268e 286 }
Yo_Robot 1:48f417da268e 287
Yo_Robot 1:48f417da268e 288
Yo_Robot 1:48f417da268e 289 /// Makes matrix Bigger!
Yo_Robot 1:48f417da268e 290 void Matrix::Resize( int Rows, int Cols )
Yo_Robot 1:48f417da268e 291 {
Yo_Robot 2:493402568a5e 292 _nRows = Rows; //Decreases one because internally
Yo_Robot 2:493402568a5e 293 _nCols = Cols; // Index starts at zero.
Yo_Robot 2:493402568a5e 294
Yo_Robot 2:493402568a5e 295 _matrix.resize( _nRows );
Yo_Robot 2:493402568a5e 296
Yo_Robot 1:48f417da268e 297 for( int i = 0; i< _nRows ; i++ )
Yo_Robot 1:48f417da268e 298 _matrix[i].resize(_nCols);
Yo_Robot 2:493402568a5e 299
Yo_Robot 2:493402568a5e 300 _pRow = 0; // If matrix is resized the <<
Yo_Robot 2:493402568a5e 301 _pCol = 0; // operator overwrites everything!
Yo_Robot 1:48f417da268e 302 }
Yo_Robot 1:48f417da268e 303
Yo_Robot 1:48f417da268e 304
Yo_Robot 1:48f417da268e 305 /// Ask user for elemnts in Matrix
Yo_Robot 0:3abd8c2d7c34 306 void Matrix::FillMatrix()
Yo_Robot 0:3abd8c2d7c34 307 {
Yo_Robot 1:48f417da268e 308 for(int i = 0; i < _nRows; i++)
Yo_Robot 0:3abd8c2d7c34 309 {
Yo_Robot 1:48f417da268e 310 for(int j = 0; j < _nCols; j++)
Yo_Robot 0:3abd8c2d7c34 311 {
Yo_Robot 0:3abd8c2d7c34 312 printf( "Position [%u][%u]: ", i, j );
Yo_Robot 0:3abd8c2d7c34 313 float numero;
Yo_Robot 0:3abd8c2d7c34 314 scanf( "%f", &numero );
Yo_Robot 0:3abd8c2d7c34 315 printf("%.3f ", numero);
Yo_Robot 1:48f417da268e 316 this->_matrix[i][j] = numero;
Yo_Robot 0:3abd8c2d7c34 317 }
Yo_Robot 0:3abd8c2d7c34 318 printf("\n");
Yo_Robot 0:3abd8c2d7c34 319 }
Yo_Robot 0:3abd8c2d7c34 320 printf("\n");
Yo_Robot 2:493402568a5e 321
Yo_Robot 2:493402568a5e 322 _pRow = _nRows;
Yo_Robot 2:493402568a5e 323 _pCol = _nCols;
Yo_Robot 0:3abd8c2d7c34 324 }
Yo_Robot 0:3abd8c2d7c34 325
Yo_Robot 0:3abd8c2d7c34 326
Yo_Robot 1:48f417da268e 327 /// Prints out Matrix.
Yo_Robot 5:a4014ab0a8cf 328 void Matrix::print() const
Yo_Robot 0:3abd8c2d7c34 329 {
Yo_Robot 1:48f417da268e 330 for( int i = 0; i < _nRows; i++ )
Yo_Robot 0:3abd8c2d7c34 331 {
Yo_Robot 1:48f417da268e 332 for( int j = 0; j < _nCols; j++ )
Yo_Robot 0:3abd8c2d7c34 333 {
Yo_Robot 0:3abd8c2d7c34 334 printf( "%.3f, ",_matrix[i][j] );
Yo_Robot 1:48f417da268e 335
Yo_Robot 0:3abd8c2d7c34 336 }
Yo_Robot 1:48f417da268e 337 printf( "\n" );
Yo_Robot 0:3abd8c2d7c34 338 }
Yo_Robot 0:3abd8c2d7c34 339 }
Yo_Robot 0:3abd8c2d7c34 340
Yo_Robot 0:3abd8c2d7c34 341
Yo_Robot 1:48f417da268e 342 /// Fills matrix with zeros.
Yo_Robot 1:48f417da268e 343 void Matrix::Clear()
Yo_Robot 1:48f417da268e 344 {
Yo_Robot 1:48f417da268e 345 for( int i = 0; i < _nRows; i++ )
Yo_Robot 1:48f417da268e 346 for( int j = 0; j < _nCols; j++ )
Yo_Robot 1:48f417da268e 347 _matrix[i][j] = 0;
Yo_Robot 2:493402568a5e 348
Yo_Robot 2:493402568a5e 349 _pCol = 0; // New data can be added
Yo_Robot 2:493402568a5e 350 _pRow = 0;
Yo_Robot 1:48f417da268e 351 }
Yo_Robot 0:3abd8c2d7c34 352
Yo_Robot 1:48f417da268e 353 /********************************************************************************/
Yo_Robot 1:48f417da268e 354
Yo_Robot 2:493402568a5e 355
Yo_Robot 1:48f417da268e 356 /// Inserts a Single element in a desired Position( Index starts at [1][1] );
Yo_Robot 1:48f417da268e 357 void Matrix::add(int Row, int Col, float number)
Yo_Robot 1:48f417da268e 358 {
Yo_Robot 1:48f417da268e 359 --Col; --Row;
Yo_Robot 2:493402568a5e 360
Yo_Robot 1:48f417da268e 361 if( Row > _nRows || Col > _nCols )
Yo_Robot 1:48f417da268e 362 {
Yo_Robot 1:48f417da268e 363 printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()");
Yo_Robot 2:493402568a5e 364
Yo_Robot 1:48f417da268e 365 }else{
Yo_Robot 1:48f417da268e 366 _matrix[Row][Col] = number;
Yo_Robot 1:48f417da268e 367 }
Yo_Robot 1:48f417da268e 368 }
Yo_Robot 1:48f417da268e 369
Yo_Robot 1:48f417da268e 370
Yo_Robot 1:48f417da268e 371 /// Adds all elements in matrix and returns the answer.
Yo_Robot 5:a4014ab0a8cf 372 float Matrix::sum() const
Yo_Robot 0:3abd8c2d7c34 373 {
Yo_Robot 5:a4014ab0a8cf 374 float total = 0;
Yo_Robot 0:3abd8c2d7c34 375
Yo_Robot 5:a4014ab0a8cf 376 for( int i = 0; i < _nRows; i++ )
Yo_Robot 5:a4014ab0a8cf 377 for( int j = 0; j < _nCols; j++ )
Yo_Robot 5:a4014ab0a8cf 378 total += _matrix[i][j];
Yo_Robot 0:3abd8c2d7c34 379 return total;
Yo_Robot 0:3abd8c2d7c34 380 }
Yo_Robot 0:3abd8c2d7c34 381
Yo_Robot 0:3abd8c2d7c34 382
Yo_Robot 1:48f417da268e 383 /// Returns the specified element. Index Starts at [1][1].
Yo_Robot 5:a4014ab0a8cf 384 float Matrix::getNumber( int Row, int Col ) const
Yo_Robot 1:48f417da268e 385 { return this->_matrix[Row -1][Col - 1]; }
Yo_Robot 0:3abd8c2d7c34 386
Yo_Robot 1:48f417da268e 387 /// Returns the number of Rows in Matrix.
Yo_Robot 5:a4014ab0a8cf 388 int Matrix::getRows() const{ return this->_nRows; }
Yo_Robot 0:3abd8c2d7c34 389
Yo_Robot 0:3abd8c2d7c34 390
Yo_Robot 1:48f417da268e 391 /// Returns the number of Columns in Matrix.
Yo_Robot 5:a4014ab0a8cf 392 int Matrix::getCols() const{ return this->_nCols; }