简体   繁体   中英

c++ use 1D Array with 2D Data

I do not use any matrix library, but instead plain std::vector for my matrix data.

To fill it with 2D data I use this code:

data[iy + dataPointsY * ix] = value;

I would like to know is this is correct or if it must be the other way (ix first). To my understanding fftw needs 'Row-major Format'. 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:

data[ix + iy*dataPointsY]

The point of -major is, when the combined index increased by 1, the corresponding 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. The naming is due to different multidimensional array indexing scheme between the two language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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