C++: Creating Matrix template using vector


 
Thread Tools Search this Thread
Top Forums Programming C++: Creating Matrix template using vector
# 1  
Old 03-03-2014
C++: Creating Matrix template using vector

I want to create a Matrix template that uses vector. For the time being I want to create the following operations. I need setting the implementation for the operations.
Maybe I do not have to use a pointer either.



Code:
template <class T>
class Matrix {

protected:

  typedef vector<T>*  VP;

  int  M;    ///< Number of rows
  int  N;    ///< Number of column
  VP*  MAT;  ///< Matrix object (pointer to vector)

public:

  ////////////////////////////////////////////////
  /// \brief Creates a matrix
  /// Matrix b;

  Matrix
  (
   );

  ////////////////////////////////////////////////
  /// \brief Creates a matrix of size (m,n), 
  /// Matrix b(3,2);

  Matrix
  (
     const int  m,
     const int  n
   );

  ////////////////////////////////////////////////
  /// \brief Sets a matrix to another matrix
  /// Matrix b(a);

  Matrix
  (
     const Matrix&  a
   );

  ////////////////////////////////////////////////
  /// \brief Destroys a matrix

  ~Matrix 
  (
   );

};


template <class T>
inline 
Matrix<T>::Matrix
(
) {

  M = N = 0;

}


template <class T>
inline
Matrix<T>::Matrix
(
 const int  m,
 const int  n
 ) {

  M = m;
  N = n;
  MAT = new VP[M];

  for (int i = 0;  i < M;  i++)
    {
      MAT[i] = new vector<T> (N);
    }

}


template <class T>
inline
Matrix<T>::Matrix
(
 const Matrix<T>&  a
 ) {

  M = a.M;
  N = a.N;
  MAT = new VP[M];

  for (int i = 0;  i < M;  i++)
    {
      MAT[i] = new vector<T> (N);
    }

}


Last edited by kristinu; 03-03-2014 at 11:14 AM..
# 2  
Old 03-03-2014
Why not use the already existing vector class?
# 3  
Old 03-03-2014
Quote:
Originally Posted by Corona688
Why not use the already existing vector class?
That's what I want to do. I have some code that uses its own vector class. Now want to change it to use std:: vector

---------- Post updated at 10:46 AM ---------- Previous update was at 10:24 AM ----------

Have now started modifications to use vector.

Current problem is what to do instead of calling

Code:
MAT[i] = new vector<T> (N);

Code:
template <class T>
class Matrix {

private:
  std::vector< std::vector<T> > MAT;

protected:

  unsigned  M;  ///< Number of rows
  unsigned  N;  ///< Number of column

public:

  ////////////////////////////////////////////////
  /// \brief Creates a matrix
  /// Matrix b;

  Matrix
  (
   );

  ////////////////////////////////////////////////
  /// \brief Creates a matrix of size (m,n), 
  /// Matrix b(3,2);

  Matrix
  (
     const int  m,
     const int  n
   );

  ////////////////////////////////////////////////
  /// \brief Sets a matrix to another matrix
  /// Matrix b(a);

  Matrix
  (
     const Matrix&  a
   );

  ////////////////////////////////////////////////
  /// \brief Destroys a matrix

  ~Matrix 
  (
   );

  ////////////////////////////////////////////////
  /// \brief      Return a reference to element i in a vector
  /// \param[in]  i

  Vector<T>&  
  operator [] 
  (
     const int  i
   );

  ////////////////////////////////////////////////
  /// \brief      Return a reference to element i in a vector
  /// \param[in]  i

  vector<T>&  
  operator [] 
  (
     const int  i
   ) const;


};

---------- Post updated at 11:01 AM ---------- Previous update was at 10:46 AM ----------

Not sure how much this will work

Code:
template <class T>
inline

Matrix<T>::Matrix
(
 const unsigned  m,
 const unsigned  n
 ) {

  M = m;
  N = n;

  for (unsigned i = 0;  i < M;  i++)
    {
      MAT[i].resize (N);
    }

}


template <class T>
inline

Matrix<T>::Matrix
(
 const Matrix<T>&  a
 ) {

  M = a.M;
  N = a.N;

  for(int i = 0; i<M; i++)
    {
      vector<T>  v;
      for(int j = 0; j<N; j++)
        {
          v.push_back(a[i][j]);
        }
      MAT.push_back(v);
    }

}


Last edited by kristinu; 03-03-2014 at 01:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating verbal structures from a dictionary and a template

My main aim here is to create a database of verbs in a language to Hindi. The output if it works well will be put up on a University site for researchers to use for Machine Translation. This because one of the main weaknesses of MT is in the area of verbs. Sorry for the long post but the problem... (4 Replies)
Discussion started by: gimley
4 Replies

2. Solaris

Creating a VMware template

Hello, I am creating a Solaris 11 template on my ESXI host. I would like each VM that is deployed from the template to have its own unique host fingerprint. With Linux, I simply delete host keys, which causes new keys to be generated at bootup (new VM deployment) Is there a way to do this... (1 Reply)
Discussion started by: firefoxx04
1 Replies

3. Shell Programming and Scripting

Creating a matrix out of a longitudinal data set

Hi I do have a tab delimited file with 2 columns, which is stratified based on the first column. There are 1000's of values in the file. Below is an example of the input file 1 AB 1 AC 1 CC 1 DD 2 AB 2 CC 2 AC 2 AB 3 CF 3 CC 3 DD 4 AC 4 CC 4 AD (5 Replies)
Discussion started by: Kanja
5 Replies

4. Shell Programming and Scripting

Creating matrix from folders and subfolders

Hello, Greetings! please help me produce the following solution. I need to produce one big matrix file from several files in different levels. If it helps, the index folder provides information on chromosome index and the data folder provides information on values for chromosomes. there... (8 Replies)
Discussion started by: newbie83
8 Replies

5. Shell Programming and Scripting

Perl- creating a matrix from a 3 column file

Dear all, I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the... (2 Replies)
Discussion started by: gabrysfe
2 Replies

6. Shell Programming and Scripting

Creating Matrix from file

Hi all, I'm a newbie in shell scripting and currently I'm trying to create a matrix using bash. The Output will look like this AB CDE FG 1 2 3 4 5 6 7 I'm stuck on the ABCDEFG display. printFlightSeats() { rows=7 columns=7 for ((i=0;i<=$rows;i++)) do (2 Replies)
Discussion started by: vinzping
2 Replies

7. UNIX for Dummies Questions & Answers

BASH - Creating a Matrix

I'm trying to create a Matrix using bash. The expected output is .AB CDE FG 1 2 3 4 5 6 7 I'm a newbie in shell language, really appreciate if there is anyone who can guide me with this. Double post again, continued here (0 Replies)
Discussion started by: vinzping
0 Replies

8. Ubuntu

Creating Matrix

Hi all, I'm a newbie in shell scripting and currently I'm trying to create a matrix using bash. The Output will look like this AB CDE FG 1 2 3 4 5 6 7 I'm stuck on the ABCDEFG display. printFlightSeats() { rows=7 columns=7 for ((i=0;i<=$rows;i++)) do (0 Replies)
Discussion started by: vinzping
0 Replies

9. Shell Programming and Scripting

Creating a matrix from files.

I need to create a large matrix so that I can feed that matrix to MATLAB for processing. The problem is creating that matrix because my data is completely scattered around files. 1. I have one big dictionary file which has words in newlines, like apple orange pineapple 2. I have some... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

10. UNIX for Advanced & Expert Users

Creating new system Makefile template

I am attempting to set-up a Makefile to use for a new system on a Sun Unix machine. I am new to creating Makefiles. I am trying to start simply by compiling a program. I am getting the following error message but an uncertain what 'Error Code 1' is. Is there a web site with Error Codes... (1 Reply)
Discussion started by: CaptainRo
1 Replies
Login or Register to Ask a Question