问题描述
如何将尺寸 1×2N 的数据重塑为Eigen中的复杂形式,以形成 P×Q 复矩阵,具有 N 复数, P×Q = N ?在数据中,实部和虚部彼此相邻。我想动态重塑 data ,因为数据可以具有不同的大小。我试图防止复制,只是将数据映射到复杂类型。
How can I reshape data of size 1×2N to a complex form in Eigen to a form a P×Q complex matrix, with N complex numbers, P×Q=N? In data, the real and imaginary parts are right next to each other. I would like to dynamically reshape data as the data can have different sizes. I am trying to prevent copying and just map the data to complex type.
int N = 9; int P = 3; int Q = 6; float *data = new float[2*N]; for(int i = 0; i < 2*N; i++) data[i] = i + 1; // data = {1, 2, 3, 4, ..., 17, 18}; Eigen::Map<Eigen::MatrixXcf> A(data, P, Q); // trying to have something like this. // Desired reshaping: // A = [ // 1 + 2i 7 + 8i 13 + 14i // 3 + 4i 9 + 10i 15 + 16i // 5 + 6i 11 + 12i 17 + 18i // ]
我尝试先将数据转换为复杂的本征数组(最终转换为 MatrixXcf ),
I tried to first convert data to a complex Eigen array (to ultimately convert to MatrixXcf), which does not work either:
Eigen::Map<Eigen::ArrayXf> Arr(data, N); // this works Eigen::Map<Eigen::ArrayXcf> Arrc(A.data(), N); // trying to map data to an Eigen complex array.
可以在大步 > Eigen :: Map 有帮助吗?
Could stride in Eigen::Map be helpful?
最简单的解决方案是遍历所有元素并转换 data 到 std :: complex< float>数组* datac =新的std :: complex< float> [N]; 。我想知道Eigen是否可以将 data 映射到 datac 。
The simplest solution is to loop through all the elements and convert data to an array of std::complex<float> *datac = new std::complex<float>[N];. I was wondering if Eigen can map data to datac. Thanks in advance.
推荐答案
这是MCVE答案(),以及一些其他示例,说明了如何使用跨步获得不同的结果:
Here is the MCVE answer (online example) with some extra examples of how you can use the stride to get different outcomes:
#include "Eigen/Core" #include <iostream> #include <complex> int main() { int N = 9; int P = 3; int Q = 6; float *data = new float[20*N]; for(int i = 0; i < 20*N; i++) data[i] = i + 1; // data = {1, 2, 3, 4, ..., 170, 180}; // Produces the output of the "Desired reshaping" Eigen::Map<Eigen::MatrixXcf> A((std::complex<float>*)(data), P, P); std::cout << A << "\n\n"; // Produces what you originally wrote (plus a cast so it works) Eigen::Map<Eigen::MatrixXcf> B((std::complex<float>*)(data), P, Q); std::cout << B << "\n\n"; // Start each column at the 10xJ position Eigen::Map<Eigen::MatrixXcf, 0, Eigen::OuterStride<>> C((std::complex<float>*)(data), P, Q, Eigen::OuterStride<>(10)); std::cout << C << "\n\n"; // Skip every other value Eigen::Map<Eigen::MatrixXcf, 0, Eigen::InnerStride<>> D((std::complex<float>*)(data), P, Q, Eigen::InnerStride<>(2)); std::cout << D << "\n\n"; delete [] data; return 0; }
输出为:
(1,2)(7,8)(13,14)( 19,20)(25,26)(31,32)
(3,4)(9,10)(15,16)(21,22)(27,28)(33,34 )
(5,6)(11,12)(17,18)(23,24)(29,30)(35,36)
(1,2) (7,8) (13,14) (19,20) (25,26) (31,32)
(3,4) (9,10) (15,16) (21,22) (27,28) (33,34)
(5,6) (11,12) (17,18) (23,24) (29,30) (35,36)
(1,2)(21,22)(41,42)(61,62)(81,82)(101,102)
(3,4)(23,24)(43 ,44)(63,64)(83,84)(103,104)
(5,6)(25,26)(45,46)(65,66)(85,86)(105,106) )
(1,2) (21,22) (41,42) (61,62) (81,82) (101,102)
(3,4) (23,24) (43,44) (63,64) (83,84) (103,104)
(5,6) (25,26) (45,46) (65,66) (85,86) (105,106)
(1,2)(13,14)(25,26)(37,38)(49,50)(61,62)
(5,6)(17,18)(29,30)(41,42)(53,54)(65,66)
(9,10)(21,22) (33,34)(45,46)(57,58)(69,70)
(1,2) (13,14) (25,26) (37,38) (49,50) (61,62)
(5,6) (17,18) (29,30) (41,42) (53,54) (65,66)
(9,10) (21,22) (33,34) (45,46) (57,58) (69,70)
这篇关于将平面数组重塑为复杂的本征类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!