Sparse Matrix's Transpose

原创 2016年05月31日 15:00:23

Sparse Matrix

Usually, suppose that there is m rows and n cols, and t non-zero element in a matrix. Make δ=t/(mn), δ is called sparse factor. If δ0.05, we could call the matrix a sparse matrix.

Transpose Algorithm

  • The data is ordered by row.

About classical method:

  • According to matrix’s diagonal, making half of number of all element( munu) tranpose is a easy way to realize matrix’s transpose.

    Time Complexity: O(munu)


There are two methods.
  • First

    Requirement:
    The result must orderly by col. If don’t require the result is orderly, We could do this in time O(nu).

    Description:
    From first row, find the elem whose row-num(i) is this row, and transpose to col.

    // M is source Matrix, T is target Matrix
    // mu is row-num, nu is col-num, tu is non-zero-num
    // .i is row, .j is col, .e is elem
    
    Status TranposeMatrix(Matrix M, Matrix &T) {
        T.mu = M.nu, T.nu = m.Mu, T.tu = M.tu;
        if (T.mu) {
            q = 1;
            for(col = 1; col <= M.nu; ++col)
            for(p = 1; p <= M.tu; ++p) 
                if(M.data[p].j == col) {
                    T.data[q].i = M.data[p].i;
                    T.data[q].j = M.data[p].j;
                    T.data[q].e = M.data[p].e;
                    ++q;
                }
        }
        return OK;
    }

    Obviously, there is nutu times operation.

    Time Complexity: O(nutu)

    If tumu, this method is bad.

  • second

    Name: Fast Transpose

    Description:
    If we could determine every col of result serial number, we could make every data right location.

    Known:
    cpot[1] = 1;
    cpot[col] = cpot[col- 1] + num[col- 1];

    Because the source data is ordered by row, we could make every col a point to mark this col’s the data sequence.

    Status FastTransposeMatrix(Matrix M, Matrix &T) {
        T.mu = M.nu; T.nu = M.mu; T.tu = M.tu;
        if(T.tu) {
            for(col = 1; col <= M.nu; ++col) num[col] = 0;
            for(t = 1; t <= M.tu; ++t) ++num[M.data[t].j];
            cpot[i] = 1;
    
            for (col = 2; col <= M.nu; ++col) cpot[col] = cpot[col - 1] + num [col - 1];
            for (p= 1; p<= M.tu; ++p) {
                col = M.data[p].j;
                q = cpot[col];
                T.data[q].i = M.data[p].j;
                T.data[q].j = M.data[p].i;
                T.data[q].e = M.data[p].e;
                ++ cpot[col];
            }
        }
    }

    Obviously, there is nu+tu times operation.

    Time Complexity: O(nu+tu)

    In worst situation, tu=munu, this time is O(munu), it still works well.

版权声明:新建了专注于语义分割的QQ群704803384,欢迎交流!!!

Matrix Transpose

点击打开链接   A: Matrix Transpose  A matrix is a rectangular array of element...
  • Dinivity123
  • Dinivity123
  • 2013年04月07日 18:38
  • 1084

[leetcode] 311. Sparse Matrix Multiplication 解题报告

题目链接: https://leetcode.com/problems/sparse-matrix-multiplication/ Given two sparse matrices A and ...
  • qq508618087
  • qq508618087
  • 2016年03月13日 04:19
  • 2975

[leetcode 311] Sparse Matrix Multiplication

Question: Given two sparse matrices A and B, return the result of AB. You may assume that ...
  • Xiaohei00000
  • Xiaohei00000
  • 2015年12月29日 22:45
  • 1986

Inverse transpose matrix

对normal进行变换不能直接乘以变换矩阵,必须乘以inverse transpose matrix 给定一个顶点的Normal n,以及过该点的切平面上的一个向量u,且有变换矩阵T。u,n是行向量...
  • seizeF
  • seizeF
  • 2011年08月21日 18:16
  • 2688

[LeetCode311]Sparse Matrix Multiplication

Given two sparse matrices A and B, return the result of AB.You may assume that A's column number is ...
  • u012175043
  • u012175043
  • 2015年11月29日 07:33
  • 1940

sklearn sparse matrix 的构造方法

1、SciPy中 sparse matrix的格式: 2、写入稀疏矩阵,一个元素,一个元素写入。 from scipy.sparse import * from scipy import * ...
  • lujiandong1
  • lujiandong1
  • 2016年02月14日 17:01
  • 1519

matlab-sparse函数和full函数-sparse matrix和full matrix

sparse函数 功能:Create sparse matrix-创建稀疏矩阵 用法1:S=sparse(X)——将矩阵X转化为稀疏矩阵的形式,即矩阵X中任何零元素去除,非零元素及其下标(索引)组...
  • meng4411yu
  • meng4411yu
  • 2013年04月23日 18:06
  • 13497

LeetCode 311. Sparse Matrix Multiplication(稀疏矩阵相乘)

原题网址:https://leetcode.com/problems/sparse-matrix-multiplication/ Given two sparse matrices A and B...
  • jmspan
  • jmspan
  • 2016年04月21日 08:12
  • 1519

Gradient Boosting Classifier sparse matrix issue using pandas and scikit

I have been using the following code to do multiclass classification which uses GradientBoostingClas...
  • suibianshen2012
  • suibianshen2012
  • 2016年10月19日 16:17
  • 691

numpy中,矩阵的三种转置transpose、getT、getH的区别

由于没有时间整理,先贴出官方帮助文档,以便知道对矩阵有这三种转置操作,之间的差别以后遇到问题了再整理。 help(np.matrix.H) Help on property:     Ret...
  • u010852680
  • u010852680
  • 2017年09月03日 14:52
  • 223
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Sparse Matrix's Transpose
举报原因:
原因补充:

(最多只允许输入30个字)