C++实现矩阵压缩

转置运算时一种最简单的矩阵运算。对于一个m*n的矩阵M,他的转置矩阵T是一个n*m的矩阵,且T(i,j) = M(j,i).

一个稀疏矩阵的转置矩阵仍然是稀疏矩阵。

矩阵转置

方案一:

1将矩阵的行列值相互交换

2将每个原则中的i j 相互交换

3重新排列三元组之间的次序

这种方法实现比较简单,一次迭代交换i j 值。

然后就是两层循环进行排序操作了。

方案二

具体实心步骤:

1 迭代遍历,统计列中元素个数

2 由1的结果迭代计算每一列中元素起始位置

3 依据2中得到数据进行转置操作

代码实现如下

//稀疏矩阵
#pragma once
#include <vector>
template<class T>
class Trituple
{
public:
Trituple(int row, int col, T& n)
:_row(row)
, _col(col)
, _value(n)
{}
int _row;
int _col;
T _value;
}; template<class T>
class SparseMatrix//稀疏矩阵
{
public:
SparseMatrix(T*a, const int m, const int n, T& invalid)///行序排列
:_rowSize(m)
, _colSize(n)
, _invalid(invalid)
{
size_t index = ;
for (int i = ; i < _rowSize; ++i)
{
for (int j = ; j < _colSize; ++j)
{
if (a[i*n + j] != _invalid)
{
_array.push_back(*(new Trituple<T>(i, j, a[i*n + j])));
//_array[index++] = (new Trituple<T>(i,j,a[i*n+j]));
}
}
}
} void Print()
{
size_t index = ;
for (int i = ; i < _rowSize; ++i)
{
for (int j = ; j < _colSize; ++j)
{
//if (_array[i*_colSize + j]._value != _invalid)
if ((_array[index]._row == i)&&(_array[index]._col == j))
{
cout << _array[index++]._value << "->";
}
else
{
cout << _invalid<<" ->";
}
}
cout << endl;
}
cout << endl;
} //转置方案:交换行列大小 值,交换元祖内部行列值. 重新排序vector;
SparseMatrix<T> Transpose()
{
SparseMatrix<T> x(*this);
::swap(x._rowSize, x._colSize);
x._array.clear();
int i = ;
for (int j = ; j < _colSize; ++j)
{
i = ;
while (i < _array.size())
{
if (j == _array[i]._col)
{
//////////////////////////////////////////////////////////////////
//Trituple<T> t(_array[i]._row, _array[i]._col, _array[i]._value);
Trituple<T> t(_array[i]._col, _array[i]._row, _array[i]._value);
x._array.push_back(t);
}
++i;
}
}
return x;
}
SparseMatrix<T> FastTranspose()
{
//①:计算并保存每一列中非0元的个数;
//②:求col列中第一个非0元在矩阵中的行位置。
//③:依据以上,进行插入操作,并且更新cpot中的值
SparseMatrix<T> x(*this);
x._colSize = _rowSize;
x._rowSize = _colSize;
x._invalid = _invalid;
if (_array.size())
{
int* RowCount = new int[_colSize]; //列中元素数
int* RowStart = new int[_colSize]; //列中元素起始位置
memset(RowCount, , sizeof(int)*_colSize);
memset(RowStart, , sizeof(int)*_colSize);
int index = ;
while (index < _array.size()) //一次迭代O(n)
{
++RowCount[_array[index++]._col];
}
index = ;
while (index < _colSize) //O(n)
{
RowStart[index] = RowStart[index - ] + RowCount[index - ];
++index;
}
//执行快速转置
int i = ;
while (i < _array.size()) //两次迭代 O(n)
{
int col = _array[i]._col;
int start = RowStart[col];
x._array[start]._row = _array[i]._col;
x._array[start]._col = _array[i]._row;
x._array[start]._value = _array[i]._value;
++RowStart[col];
i++;
}
delete[]RowCount;
delete[]RowStart;
}//if
return x;
}
~SparseMatrix()
{
}
private:
vector<Trituple<T> > _array;
size_t _rowSize;
size_t _colSize;
T _invalid;
};

C++实现矩阵压缩的更多相关文章

  1. [Swust OJ 589]--吃西瓜(三维矩阵压缩)

    题目链接:http://acm.swust.edu.cn/problem/589/ Time limit(ms): 2000 Memory limit(kb): 65535   Description ...

  2. Poj 3318 Matrix Multiplication( 矩阵压缩)

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18928   Accepted: ...

  3. 【矩阵压缩】 poj 1050

    题意:给一个矩阵,里面有正负数,求子矩阵和的最大值 #include <iostream> #include <cstdio> #include <stdlib.h> ...

  4. To the Max(矩阵压缩)

    To the Max Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Su ...

  5. 矩阵压缩写法 scipy spark.ml.linalg里都有,CRS,CCS

    CRS 表示:Compressed Row Storage CCS 表示:Compressed Column Storage CRS的表示参考: https://blog.csdn.net/buptf ...

  6. 强大的矩阵奇异值分解(SVD)及其应用

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  7. 机器学习中的数学-矩阵奇异值分解(SVD)及其应用

    转自:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/19/svd-and-applications.html 版权声明: 本文由LeftNotE ...

  8. 机器学习中的数学(5)-强大的矩阵奇异值分解(SVD)及其应用

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  9. 高速压缩跟踪(fast compressive tracking)(CT)算法分析

    本文为原创,转载请注明出处:http://blog.csdn.net/autocyz/article/details/44490009 Fast Compressive Tracking (高速压缩跟 ...

随机推荐

  1. [golang note] 数组切片

    数组 √ golang数组包含的每个数据称为数组元素(element),数组包含的元素个数被称为数组长度(length). √ golang数组的长度在定义后不可更改,并且在声明时可以是一个常量或常量 ...

  2. beego——session模块

    session介绍 session是一个独立的模块,即你可以那这个模块应用于其它Go程序中. session模块是用来存储客户端用户,session目前只支持cookie方式的请求,如果客户端不支持c ...

  3. Java基础知识陷阱(二)

    本文发表于本人博客. 上次说了一些关于字符串的知识,都是比较基础的,那这次也说下关于对象地址问题,比如传参.先看下面代码: public void changeInt(int a){ a = ; } ...

  4. SQL Server排名函数与排名开窗函数

    什么是排名函数?说实话我也不甚清楚,我知道 order by 是排序用的,那么什么又是排名函数呢? 接下来看几个示例就明白了. 首先建立一个表,随便插入一些数据. ROW_NUMBER 函数:直接排序 ...

  5. 001-搭建spring boot项目

    1.第一步.file--new--project. 2.spring initializr--project sdk--default--next 3. 4.spring boot--选择依赖项--n ...

  6. 国内Hadoop相关的开源项目

    1.BC-Hadoop:中国移动Hadoop工具链打包 https://github.com/cmri/bc-hadoop2.0 孵化阶段,将成为一个通用的开源Hadoop平台 2.BC-BSP:中国 ...

  7. 洛谷 P4211 [LNOI2014]LCA (树链剖分+离线)

    题目:https://www.luogu.org/problemnew/solution/P4211 相当难的一道题,其思想难以用言语表达透彻. 对于每个查询,区间[L,R]中的每个点与z的lca肯定 ...

  8. mysql调优小记

    对于INNODB,主键就是聚集索引,如果没有主键定义,则第一个唯一非空索引被作为聚集索引.如果没有主键也没有合适的唯一索引,那么innodb内部会生成一个隐藏的主键作为聚集索引,这个隐藏的主键类似一个 ...

  9. javascript 理解对象--- 属性类型

    ECMA-262 把对象定义为:无序属性的集合,其属性可以包含基本值.对象或者函数: var Person = { name:"wsc", age :"25", ...

  10. U盘在制作Ubuntu启动盘后Windows系统下显示空间不对的解决办法(Ubuntu系统下格式化U盘的方法)

    用Ubuntu系统自带的启动盘制作工具后,将U盘拿到Windows系统下使用显示出的空间与U盘大小不同. 解决该问题的办法: 使用Linux终端: 第一步:sudo fdisk -l 这个命令主要是查 ...