简体   繁体   English

c ++使用带有2D数据的1D数组

[英]c++ use 1D Array with 2D Data

I do not use any matrix library, but instead plain std::vector for my matrix data. 我不使用任何矩阵库,而是将纯std :: vector用于矩阵数据。

To fill it with 2D data I use this code: 要使用2D数据填充它,请使用以下代码:

data[iy + dataPointsY * ix] = value;

I would like to know is this is correct or if it must be the other way (ix first). 我想知道这是正确的,还是必须以其他方式(第ix项)进行。 To my understanding fftw needs 'Row-major Format'. 据我了解,fftw需要“行大格式”。 Since I use it the formula should be according to row-major format. 由于我使用了该公式,因此公式应采用行优先格式。

Assuming you want row major format for fftw , what you want is: 假设您想要fftw行主格式,那么您需要的是:

data[ix + iy*dataPointsY]

The point of row -major is, when the combined index increased by 1, the corresponding row index would be same (assuming not overflowing to the next row). -major的要点是,当组合索引增加1时,对应的索引将相同(假设不会溢出到下一行)。

double m[4][4];
mp = (double*)m;
mp[1+2*3] == m[2][1]; //true
mp[2+2*3] == m[2][2]; //true
mp[2+2*3] == m[3][1]; //false

In general, there's no "right" way to store a matrix. 通常,没有“正确”的方式来存储矩阵。 Row major format is also called "C-style" matrix, while column major is called "fortran-style" matrix. 行主格式也称为“ C样式”矩阵,而列主格式也称为“ fortran样式”矩阵。 The naming is due to different multidimensional array indexing scheme between the two language. 命名是由于两种语言之间的多维数组索引方案不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM