-
Notifications
You must be signed in to change notification settings - Fork 557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std::vector<std::vector<double>> to NdArray<double> #59
Comments
So after I posted this I found out that this seems to work: std::vector<std::vector<double>> vv(n_samples, std::vector<double>(n_samples, 0.0));
auto s = nc::NdArray<double>(n_samples, n_samples);
for (int i = 0; i < n_samples; ++i) {
s(i, s.cSlice()) = nc::fromiter<double>(vv[i].begin(), vv[i].end());
} Is this the proper way to do it? Even so there should be a constructor still. |
No, what you have above will not work. Slicing operations in NumCpp return copies. You could modify your example by using one the overloaded put() methods. However, the most efficient way to do what you are trying to do would be to directly loop over the data. #include "NumCpp.hpp"
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
int main()
{
// create a vector<vector> and fill with monotonically increasing values
const nc::uint32 n_samples = 10;
std::vector<std::vector<double>> vv(n_samples, std::vector<double>(n_samples, 0.0));
double start = 0.0;
for (auto& row : vv)
{
std::iota(row.begin(), row.end(), start);
start += row.size();
}
// convert the vector<vector> to NdArray
auto s = nc::NdArray<double>(n_samples, n_samples);
for (nc::uint32 row = 0; row < n_samples; ++row)
{
for (nc::uint32 col = 0; col < n_samples; ++col)
{
s(row, col) = vv[row][col];
}
}
// display the array for sanity
std::cout << s;
return EXIT_SUCCESS;
} I'm working on a fairly large update at the moment that should be ready to release in 2 weeks or so. I'll add additional constructors in this update to more directly accomplish what you are trying to do. |
Added additional |
Is your feature request related to a problem? Please describe.
My existing algorithms produce a 2d matrix in the form of std::vector<std::vector> of equal size
Describe the solution you'd like
there should be a constructor to a 2D NdArray with std::vector<std::vector> as input. It parallels the existing initializer list constructor
Describe alternatives you've considered
I have not found any alternative as it seems impossible to construct what I want with the existing API. So if this functionality exists the documentation fails to explain how it is provided.
The text was updated successfully, but these errors were encountered: