0

I have the following code snippet:

Eigen::MatrixXcd Recon = Eigen::MatrixXcd::Zero(p.nPoints*p.na, p.numEl);

// Do some functions to add data to Recon

outputs[0] = factory.createArray<std::complex<double>>({static_cast<size_t>(p.numEl),static_cast<size_t>(p.na),static_cast<size_t>(p.nPoints)});
                  
for(int i = 0; i < p.na; i++) {
   for (int j = 0; j < p.nPoints; j++) {
      for (int n = 0; n < p.numEl; n++) {
         outputs[n][i][j] = Recon( i*p.nPoints + j ,n);
      }
   }
}

How can I adjust my code above so that I am mapping the data in outputs to the data in Recon instead of copying it element by element?

drakon101
  • 524
  • 1
  • 8
  • I think there is something wrong with that code. You create a 3-dimensional array in `outputs[0]` which would imply a 4-dimensional structure overall and `outputs[n][i][j]` referring to a column in that structure rather than a single element. – Homer512 Mar 20 '23 at 19:10
  • Also, since I'm unfamiliar with the Matlab API and the documentation is incredibly sparse, the [`TypedIterator`](https://ch.mathworks.com/help/matlab/apiref/matlab.data.typediterator.html) would iterate in the order `[n][j][i]` since it is a column-major format, not your desired `[n][i][j]`, correct? – Homer512 Mar 20 '23 at 19:13
  • 1
    You want to create the MATLAB array first, then create an `Eigen::Map<>` over the data. This map can be used like any other Eigen array, except that its data is the underlying MATLAB array. By default, Eigen uses the same data layout as MATLAB, so this should be simple. https://eigen.tuxfamily.org/dox/classEigen_1_1Map.html – Cris Luengo Mar 20 '23 at 19:32
  • The linked answer uses the C Data API, not the C++ Data API like you are using here. But the same concept applies, just use your `factory.createArray<>` function to create the array, get its data pointer, then create a `Eigen::Map<>` around it. – Cris Luengo Mar 20 '23 at 19:38
  • Not sure I approve of closing a question about one API with a duplicate answer using another API but here we are … If you want to stay with the new API, use its iterators and [Eigen's iterator](https://eigen.tuxfamily.org/dox/group__TutorialSTL.html) with [`std::copy`](https://en.cppreference.com/w/cpp/algorithm/copy). Note that vectorization isn't a huge deal here since you already have 16 byte elements; so even scalar code shouldn't be too bad as long as there is no function call per entry – Homer512 Mar 26 '23 at 08:18

0 Answers0