首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >矩阵向量乘法CCS c++

矩阵向量乘法CCS c++
EN

Stack Overflow用户
提问于 2016-03-07 03:01:31
回答 1查看 577关注 0票数 1

我试图用压缩柱存储中的矩阵来乘矩阵和向量。汇总表是:

代码语言:javascript
复制
0   3   0   
4   0   0   
2   0   0

以下是CCS表格:

代码语言:javascript
复制
[ 4 2 3 ]
[ 2 3 1 ]
[ 1 3 4 4 ]

矢量是:

代码语言:javascript
复制
[ 1 3 4 ]

所以产品应该是

代码语言:javascript
复制
[ 9 4 2 ]

下面是我要创建的函数

代码语言:javascript
复制
vector<int> multiply(vector<int> val,vector<int> col,vector<int> row,vector<int> v, int r){

    vector<int> x(v.size());
    for (int j=0; j<r; j++) {
        for (int i=col[j]-1; i<col[j+1]-1; i++) {
            cout<<"\n"<<val[i]<<"*"<<v[j]<<"\n";
            x[j]+= val[i]*v[j];
        }
    }
    return x;
}

但这又回来了

代码语言:javascript
复制
[ 6 9 0 ]

这是我得到的最接近真正的解决方案,我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-07 03:58:57

我认为这是由col_ptr矢量驱动的。

1.)我不确定r可以取多少值,所以我删除了它,因为我们不需要这些信息来解决问题2)。我应该注意,我没有编译这个,但是我相信算法是正确的。)有一些明显的方法可以优化这段代码的内存使用,但是我保留了这些变量来帮助解释这个过程。4.)您发布的代码的主要问题是,它似乎没有使用行数组来告诉我们值在哪一行!

代码语言:javascript
复制
vector<int> multiply(vector<int> val,vector<int> col,vector<int> row,vector<int> v) {
   vector<int> x(v.size());
   //we may need to initialize x as all zeroes, I forget
   int col_size = col.size();
   int column_idx = 0;
   for (int j=0; j<col_size-1; j++) {
      //j indicates where the columns start going!
      //columns end prior to the next column start
      //val_index is where we need to start in the val array, for this column
      int val_index_start = col[j]-1; //this is redunda
      //we keep traversing forward until the next column starts
      int next_column_start = col[j+1]-1;
      for (int k=val_index_start; k < next_column_start && k < v.size(); k++) {
        int row_of_cell = row[k] - 1;
        int column_of_cell = column_idx;
        int product = v[column_idx]*val[k];
        x[row_of_cell] += product;
      }
      column_idx++;
   }
   return x;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35835496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档