0
我一直在試圖給矩陣大小,同時通過功能運行時,例如表達必須具有恆定的值特徵矩陣
Eigen::MatrixXd FileReader::load_csv(const std::string & path, const int rows_csv, const int columns_csv) {
std::ifstream indata;
indata.open(path);
std::string line;
std::vector<double> values;
int rows = 0;
while (getline(indata, line)) {
std::stringstream lineStream(line);
std::string cell;
while (std::getline(lineStream, cell, ',')) {
values.push_back(std::stod(cell));
}
++rows;
}
std::cout << "loading";
return Eigen::Map<const Eigen::Matrix<double, columns_csv, rows_csv, Eigen::RowMajor>>(values.data(), rows, values.size()/rows);
}
(上面的代碼是從https://stackoverflow.com/a/39146048/3782963),我不能夠發送一個常數矩陣大小的值函數,我得到Expression must have constant value
錯誤。有沒有什麼辦法可以做到這樣的事情:
Eigen::MatrixXd mat = load_csv("some_path", 20, 30);
有什麼想法嗎?
您應該使用',而不是'-1'徵:: Dynamic',但是(儘管價值是不會再有變化的)。而對於防禦性編程來說,檢查每一行是否有相同數量的條目是有意義的。 – chtz
@chtz更改爲「Eigen :: Dynamic」。至於防禦性編程,在OP鏈接的問題中,這將是相關的(我如何將csv文件讀入特徵矩陣?)。在這種情況下(正如我所理解的那樣),他/她想要覆蓋文件中矩陣的形狀,而不管它是否是矩形。 –
但是不是那麼慢?如果我使用Eigen :: Dynamic,它似乎佔用了相當多的內存。 – Akshay