フィルターのクリア

Copying std::vector contents to TypedArray

72 ビュー (過去 30 日間)
David Moore
David Moore 2020 年 6 月 29 日
編集済み: Paulo Urriza 2023 年 5 月 16 日
Is there a better way to copy the contents of a std::vector to a Matlab Array in C++? The following seems long winded.
V is a std::vector in the code below.
matlab::data::ArrayFactory factory;
matlab::data::TypedArray<double> A = factory.createArray<double>({ 1, V.size() });
int i = 0;
for (auto e : V) {
A[i++] = e;
}

採用された回答

Breno Vincenzo de Almeida
Breno Vincenzo de Almeida 2020 年 9 月 20 日
I managed to do this out after hooours of testing.
matlab::data::ArrayFactory f;
matlab::data::TypedArray<double> A = f.createArray<double>({1, V.size()}, V.data(), V.data()+V.size());
Source: the `matlab::data::ArrayFactory` page in the following link C++ class to create arrays - MATLAB.
But I can't find a way for the ItType method of supplying the data to work.
  2 件のコメント
Jason Laks
Jason Laks 2021 年 2 月 19 日
The code above will not compile using the mex command from matlab's command line in R2020a. Does anyone have a code snippet that will work?
Thanks, J.
My attempt at getting the suggestion to work:
matlab::data::ArrayFactory f;
std::vector<double> V(2, 2);
matlab::data::TypedArray<double> A = f.createArray<double>({1,V.size()}, V.data(), V.data()+V.size());
Jason Laks
Jason Laks 2021 年 2 月 19 日
Wait... my apologies. The snippet above does compile.

サインインしてコメントする。

その他の回答 (1 件)

Paulo Urriza
Paulo Urriza 2023 年 5 月 16 日
編集済み: Paulo Urriza 2023 年 5 月 16 日
Type can be deduced automatically if you used the createArray overload for std::iterator.
matlab::data::ArrayFactory f;
auto A = factory.createArray({1, V.size()}, V.begin(), V.end());

カテゴリ

Help Center および File ExchangeDeploy to C++ Applications Using MATLAB Data API (C++11) についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by