線性代數-矩陣-轉置 C和C++的實現

來源:互聯網
上載者:User

標籤:this   pos   get   nbsp   code   one   一個   矩陣   his   

原理解析:

本節介紹矩陣的轉置。矩陣的轉置即將矩陣的行和列元素調換,即原來第二行第一列(用C21表示,後同)與第一行第二列(C12)元素調換位置,原來c31與C13調換。即cij與cji調換 。

(此處補圖說明)

C++語言:

首先我們想到的是把第i行第j列取出來與第j行第i列調換,這種思路很簡單就不多說了。

這裡提供另一個思路,對整行整列進行操作,方法如下:

  1. 使用getSpecifiedRow()把本矩陣中的第i行取出來放在向量tempVec中;
  2. 使用 addOneColumToBack()把上述取出來的tempVec放入臨時矩陣tempMatrix的列中;
  3. 用臨時tempMatrix取代本矩陣;
template <typename T>Matrix<T> Matrix<T>::transpose(){    Matrix<T> tempMatrix;    vector<T> tempVec;    /*get transpose*/    for(int i=0;i<m_iColumns;i++)    {        this->getSpecifiedRow(i,&tempVec);        tempMatrix.addOneColumToBack(tempVec);    }    /*swap rows and columns*/    m_vecMatrix = tempMatrix.m_vecMatrix;    int temp = m_iColumns;    m_iColumns = m_iRows;    m_iRows = temp;    return tempMatrix;}

 

線性代數-矩陣-轉置 C和C++的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.