Software & Finance





C++ - Matrix Transpose





Transpose of matrix is very easy to implement. What you need to do is swap the row elements into column elements and vice versa.

 

The following lines can do the magic, provided you have allocated memory correctly by swapping rows and cols.

 

for(int i = 0; i < m_rows; i++)

{

        for(int j = 0; j < m_cols; j++)

              m_pData[i][j] = other.m_pData[i][j];

}

 

The complete program and test run output is given below:

 


Source Code


 

#if !defined(MATRIX_H)

#define MATRIX_H

 

#include <stdio.h>

#include <iostream>

#include <tchar.h>

#include <math.h>

 

class CMatrix

{

private:

    int m_rows;

    int m_cols;

    char m_name[128];

 

    CMatrix();

public:

    double **m_pData;

 

    CMatrix(const char *name, int rows, int cols) : m_rows(rows), m_cols(cols)

    {

        strcpy(m_name, name);

        m_pData = new double*[m_rows];

        for(int i = 0; i < m_rows; i++)

            m_pData[i] = new double[m_cols];

 

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                m_pData[i][j] = 0.0;

            }

        }

    }

 

    CMatrix(const CMatrix &other)

    {

        strcpy(m_name, other.m_name);

        m_rows = other.m_rows;

        m_cols = other.m_cols;

 

        m_pData = new double*[m_rows];

        for(int i = 0; i < m_rows; i++)

            m_pData[i] = new double[m_cols];

 

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                m_pData[i][j] = other.m_pData[i][j];

            }

        }

    }

 

    ~CMatrix()

    {

        for(int i = 0; i < m_rows; i++)

            delete [] m_pData[i];

        delete [] m_pData;

        m_rows = m_cols = 0;

    }

 

    void SetName(const char *name) { strcpy(m_name, name); }

    const char* GetName() const { return m_name; }

 

    void GetInput()

    {

        std::cin >> *this;

    }

 

    void FillSimulatedInput()

    {

        static int factor1 = 1, factor2 = 2;

        std::cout << "\n\nEnter Input For Matrix : " << m_name << " Rows: " << m_rows << " Cols: " << m_cols << "\n";

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1 << " = ";

                int data = ((i + 1) * factor1) + (j + 1) * factor2;

                m_pData[i][j] = data / 10.2;

                std::cout << m_pData[i][j] << "\n";

 

                factor1 += (rand() % 4);

                factor2 += (rand() % 3);

            }

            std::cout << "\n";

        }

 

        std::cout << "\n";

    }   

 

  

    CMatrix Transpose()

    {

        CMatrix trans("TR", m_cols, m_rows);

 

        for(int i = 0; i < m_rows; i++)

        {

            for(int j = 0; j < m_cols; j++)

            {

                trans.m_pData[j][i] = m_pData[i][j];

            }

        }

        return trans;

    }

 

    friend std::istream& operator >> (std::istream &is, CMatrix &m);

    friend std::ostream& operator << (std::ostream &os, const CMatrix &m);   

};

 

std::istream& operator >> (std::istream &is, CMatrix &m)

{

    std::cout << "\n\nEnter Input For Matrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: " << m.m_cols << "\n";

    for(int i = 0; i < m.m_rows; i++)

    {

        for(int j = 0; j < m.m_cols; j++)

        {

            std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1 << " = ";

            is >> m.m_pData[i][j];

        }

        std::cout << "\n";

    }

    std::cout << "\n";

    return is;

}

 

std::ostream& operator << (std::ostream &os,const CMatrix &m)

{

    os << "\n\nMatrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: " << m.m_cols << "\n\n";

    for(int i = 0; i < m.m_rows; i++)

    {

        os << " | ";

        for(int j = 0; j < m.m_cols; j++)

        {

            char buf[32];

            double data = m.m_pData[i][j];

            if( m.m_pData[i][j] > -0.00001 &&

                m.m_pData[i][j] < 0.00001)

                data = 0;

            sprintf(buf, "%10.2lf ", data);

            os <<  buf;

        }

        os << "|\n";

    }

    os << "\n\n";

    return os;

}

 

 

#endif

 

 

int main()

{  

 

    CMatrix a("A", 5,3);

    //std::cin >> a;

    a.FillSimulatedInput();

 

    CMatrix trans = a.Transpose();

 

    std::cout << a;

    std::cout << trans;

}

 

Output


 

Enter Input For Matrix : A Rows: 5 Cols: 3

Input For Row: 1 Col: 1 = 0.294118

Input For Row: 1 Col: 2 = 0.980392

Input For Row: 1 Col: 3 = 1.86275

 

Input For Row: 2 Col: 1 = 1.56863

Input For Row: 2 Col: 2 = 2.54902

Input For Row: 2 Col: 3 = 4.11765

 

Input For Row: 3 Col: 1 = 3.92157

Input For Row: 3 Col: 2 = 5.19608

Input For Row: 3 Col: 3 = 7.05882

 

Input For Row: 4 Col: 1 = 7.2549

Input For Row: 4 Col: 2 = 9.80392

Input For Row: 4 Col: 3 = 12.3529

 

Input For Row: 5 Col: 1 = 12.6471

Input For Row: 5 Col: 2 = 14.2157

Input For Row: 5 Col: 3 = 16.7647

  

 

Matrix : A Rows: 5 Cols: 3

 

 |       0.29       0.98       1.86 |

 |       1.57       2.55       4.12 |

 |       3.92       5.20       7.06 |

 |       7.25       9.80      12.35 |

 |      12.65      14.22      16.76 |

 

 

 

Matrix : TR Rows: 3 Cols: 5

 

 |       0.29       1.57       3.92       7.25      12.65 |

 |       0.98       2.55       5.20       9.80      14.22 |

 |       1.86       4.12       7.06      12.35      16.76 |

 

   

Press any key to continue . . .