C++实现矩阵压缩

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 = 0;
        for (int i = 0; i < _rowSize; ++i)
        {
            for (int j = 0; 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 = 0;
        for (int i = 0; i < _rowSize; ++i)
        {
            for (int j = 0; 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 = 0;
        for (int j = 0; j < _colSize; ++j)
        {
            i = 0;
            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, 0, sizeof(int)*_colSize);
            memset(RowStart, 0, sizeof(int)*_colSize);
            int index = 0;
            while (index < _array.size())  //一次迭代O(n)
            {
                ++RowCount[_array[index++]._col];
            }
            index = 1;
            while (index < _colSize)      //O(n)
            {
                RowStart[index] = RowStart[index - 1] + RowCount[index - 1];
                ++index;
            }
            //执行快速转置
            int i = 0;
            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;
};
时间: 2024-08-24 13:16:46

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

hdu1081 To the max(dp 矩阵压缩)

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8947    Accepted Submission(s): 4323 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectang

矩阵压缩存储之三元组顺序表

形态: 实现: /***************************************** 稀疏矩阵的三元组顺序表存储表示 by Rowandjj 2014/5/3 ******************************************/ #include<IOSTREAM> using namespace std; #define MAXSIZE 12500//非零元个数的最大值 typedef int ElemType; typedef struct _DATA_

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

题目链接:http://acm.swust.edu.cn/problem/589/ Time limit(ms): 2000 Memory limit(kb): 65535 Description 告诉你们一个好消息,Wraith前几天天得到一块西瓜,但是是长方体形的.... Wraith发现这块西瓜长m厘米,宽n厘米,高h厘米.他发现如果把这块西瓜平均地分成m*n*h块1立方厘米的小正方体,那么每一小块都会有一个营养值(可能为负,因为西瓜是有可能坏掉的,但是绝对值不超过200). 现在Wrai

学习日志---矩阵表示及特殊矩阵压缩

矩阵类:二维数组实现!! 每一行看成一个一维数组,在放入几个集合里面即可: 用到random类,可以生产随机数,括号里是最大值: 矩阵类: public class MyMatrix {     int[][] matrix ;//矩阵数组 Random random =new Random() ;//随机数对象 //默认的构造方法,生成3*3的矩阵 public MyMatrix() {         //默认是3*3矩阵 matrix = new int[3][3]; //初始矩阵 for

【矩阵压缩】 poj 1050

题意:给一个矩阵,里面有正负数,求子矩阵和的最大值 #include <iostream> #include <cstdio> #include <stdlib.h> #include <memory.h> using namespace std; int s[105][105],dp[105],n,temp[105]; int main() { // freopen("in.txt","r",stdin); cin&

HDU1081_To The Max【矩阵压缩】

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8528    Accepted Submission(s): 4142 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectan

矩阵压缩

下三角矩阵 A[10,5]在一维数组中的下标(下标从0开始,行优先) A[10,5]前面的元素个数:第1行——1个,第2行——2个,···,第9行——9个,第10行——4个(横着看,A[10,,5]-A[10,1]=4).共49个. A[10,5]的下标:0+49=49 A[10,5]在一维数组中的下标(下标从0开始,列优先) A[10,5]前面的元素个数:第1列——n个,第2列——n-1个,···,第4列——n-3个:第5列——5个(竖着看,A[10,5]-A[5,5]=5). A[10,5]

To the Max(矩阵压缩)

To the Max Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 2 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is a

快速压缩跟踪(CT)算法剖析

Fast Compressive Tracking (快速压缩跟踪) 虽然目前有很多种的跟踪算法,但是由于姿态的变化.光照的变化.障碍物等原因的存在,导致很多算法的鲁棒性不好. 目前比较主流的跟踪算法有两种,generative  tracking algorithms(生成跟踪算法)和discriminative algorithms(判别跟踪算法). 生成跟踪算法,顾名思义边生成边跟踪.即对这一帧的样本进行学习,将学习的结果作为下一帧的分类器,达到边学习跟踪,边跟踪边学习的效果.这种跟踪算法