2017-06-15 36 views
0

我不是在C++又那麼先進,但我試圖進行聚類分析,如何返回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)...等等,其轉換爲(特徵[1],特徵[2],特徵[5]), (特徵[3],特徵[7],特徵[8],特徵[10])等等,其中特徵[i]表示數據矩陣的第i行。

問題是我不知道如何創建一個函數,將每個集合轉換爲矩陣並返回它們。

+0

包含在'vector >'中的每個'vector '可以具有不同的大小。除此之外,你不清楚你在問什麼。 – Peter

+1

我會得到/做一個矩陣類型,然後你可以有一個矩陣類型的向量。 – NathanOliver

+2

結果應該是什麼樣子?你需要一個單一的(2D)矩陣,並且想要返回一個矩陣數組(即一個三維數組 - 相似)?這似乎是可疑的... – axalis

回答

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

您的輸入似乎尚不清楚,但我們可以走這條路:(檢查功能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; 
} 
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; 
}