如何在c ++中返回不同大小的矩阵数组?

时间:2017-06-15 13:24:36

标签: c++ function vector return return-type

我在c ++方面还不是很先进,但我正在尝试进行聚类分析,

数据,向量<矢量<双>> X是M乘以T,具有M个特征和T个数据点,我试图将特征分组成集合,其中集合中的每个特征之间的距离相关性高于特定阈值。 distCorrelation函数已经通过这种方式定义。

set<vector<double>> clusterIndices(vector<vector<double>> &X, double threshold){
    vector<double> feature[X.size()];
    for(int i = 0; i < X.size(); i++){
        for(int j = 0; j < X[0].size(); j++){
            feature[i].push_back(X[i][j]);
        }
    }
    vector<vector<double>> distCorrMatrix(X.size(), vector<double> (X.size()));
    for (int i = 0; i < X.size(); i++){
        for (int j = 0; j < X.size(); j++){
            distCorrMatrix[i][j] = (distCorrelation(feature[i],feature[j]) >= threshold ? 1.0 : 0.0);
        }
    }
    set<vector<double>> rows;
    for (int i = 0; i < X.size(); i++){
        vector<int> temp;
        for (int j = 0; j < X.size(); j++){
            if (distCorrMatrix[i][j] == 1){
                temp.push_back(j);
            }
        }
        rows.insert(temp);
    }
    return rows;
}

因此,上面的代码将生成具有相互高相关性的特征集,但仅提供这些特征的索引。 也就是说,返回的行可以是(1,2,5),(3,7,8,10)...等转换为(feature [1],feature [2],feature [5]),( feature [3],feature [7],feature [8],feature [10])... etc,其中feature [i]代表数据矩阵的第i行。

问题是我不知道如何创建一个函数,将每个集合转换为矩阵并返回它们。

3 个答案:

答案 0 :(得分:0)

不,你的代码不会编译。你应该这样做:

// k is the number of clusters
vector<vector<vector<double> > > myFunction(vector<vector<double> > &X, int k) {
    vector<vector<vector<double> > > result(k);
    for (int i = 0; i < X.size(); i++){
        //do something then know X[i] belongs to cluster j
        result[j].push_back(X[i]);
    }
    return result;
}

答案 1 :(得分:0)

据我所知,你想要这个

std::vector<int> myclusteringfunction(std::vector<std::vector<double> > const &dataitems)
{
   /* assign a cluster id to each data item */
   std::vector<int> answer;
   for(i=0;i<dataitems.size();i++)
       answer.push_back( /* get the cluster id for each data item */);

   /* return the ids as a list of the same length as your input list
      eg {0, 1, 2, 1, 1, 1, 2, 2, 0, 0, 3, 1, 1, 1, 1} for four clusters */

   return answer;
}

答案 2 :(得分:-1)

您的输入似乎不清楚,但我们可以这样:(检查函数getVectorOfMatrices)

#include <vector>
#include <iostream>

/**
 * A classic 2D matrix implementation. 
 * Pay attention to the constructors and the operator=.
 */
class Matrix2D {
public:
   // Standard constructor, allocates memory and initializes.
   Matrix2D(const unsigned int rows, const unsigned int columns) 
                              : m_rows(rows), m_columns(columns) {
     m_data = new float*[rows];
     for(unsigned row = 0; row < rows; ++row) {
       m_data[row] = new float[columns];
       for (unsigned column = 0; column < columns; ++column) {
         m_data[row][column] = 0;
       }
     }
   }

   // Copy-constructor - also allocates and initializes.
   Matrix2D(const Matrix2D& rhs) {
      m_rows = rhs.m_rows;
      m_columns = rhs.m_columns;
      m_data = new float*[rhs.m_rows];
      for (unsigned row = 0; row < rhs.m_rows; ++row) {
         m_data[row] = new float[rhs.m_columns];
         for (unsigned column = 0; column < rhs.m_columns; ++column) {
           m_data[row][column] = rhs.at(row, column);
         }
      }
   }

   // Affectation operator - also allocates memory and initializes.
   Matrix2D& operator=(const Matrix2D& rhs) {
      m_rows = rhs.m_rows;
      m_columns = rhs.m_columns;
      m_data = new float*[rhs.m_rows];
      for (unsigned row = 0; row < rhs.m_rows; ++row) {
         m_data[row] = new float[rhs.m_columns];
         for (unsigned column = 0; column < rhs.m_columns; ++column) {
           m_data[row][column] = rhs.at(row, column);
         }
      }
   }

   // Used to set values in the 2D matrix 
   // NOTA : This function should check row vs m_rows and column vs m_columns
   float& at(const unsigned int row, const unsigned int column) {
     return m_data[row][column];
   }

   // Used to get values of the 2D matrix
   // NOTA : This function should check row vs m_rows and column vs m_columns
   const float at(const unsigned int row, const unsigned int column) const {
     return m_data[row][column];
   }

   // Debug tool - prints the matrix
   void print() const {
     for (unsigned row = 0; row < m_rows; ++row) {
       for (unsigned column = 0; column < m_columns; ++column) {
         std::cout << " " << m_data[row][column] << " ";
       }
       std::cout << std::endl;
     }
   }

   // Destructor - deallocates the memory
   ~Matrix2D() {
     for (unsigned int row=0; row<m_rows; ++row) {
       delete[] m_data[row];
     }
     delete[] m_data;
   }

private:
   unsigned int m_rows; // y-size
   unsigned int m_columns; // x-size
   float**  m_data; // the data
};

/*
 * Function that creates and returns a vector of 2D matrices 
 * Matrices are of different sizes
 */
std::vector<Matrix2D> getVectorOfMatrices() {
   Matrix2D m1(1,1);
   Matrix2D m2(2,2);
   Matrix2D m3(3,3);
   Matrix2D m4(4,2);


   m1.at(0, 0) = 4;
   m2.at(0, 1) = 2;
   m4.at(1, 1) = 8;

   std::vector<Matrix2D> result;

   result.push_back(m1);
   result.push_back(m2);
   result.push_back(m3);
   result.push_back(m4);
   return result;
}

/*
 * Main - simply call our function.
 */
int main () {
  std::vector<Matrix2D> vec = getVectorOfMatrices();
  for(std::vector<Matrix2D>::iterator it = vec.begin(); it != vec.end(); ++it) {
    it->print();
  }
  return 0;
}