# Sparse Matrix's Transpose

## Sparse Matrix

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

## Transpose Algorithm

• The data is ordered by row.

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

Time Complexity: O(munu)$O(mu * nu)$

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)$O(nu)$.

Description:
From first row, find the elem whose row-num(i$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$nu * tu$ times operation.

Time Complexity: O(nutu)$O(nu * tu)$

If tumu$tu \ge mu$, 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$nu + tu$ times operation.

Time Complexity: O(nu+tu)$O(nu + tu)$

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

• 本文已收录于以下专栏：

## Matrix Transpose

• Dinivity123
• 2013年04月07日 18:38
• 1084

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

• 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
• 2015年12月29日 22:45
• 1986

## Inverse transpose matrix

• 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
• 2015年11月29日 07:33
• 1940

## sklearn sparse matrix 的构造方法

1、SciPy中 sparse matrix的格式： 2、写入稀疏矩阵，一个元素，一个元素写入。 from scipy.sparse import * from scipy import * ...
• 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
• 2013年04月23日 18:06
• 13497

## LeetCode 311. Sparse Matrix Multiplication（稀疏矩阵相乘）

• 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
• 2016年10月19日 16:17
• 691

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

• u010852680
• 2017年09月03日 14:52
• 223

举报原因： 您举报文章：Sparse Matrix's Transpose 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)