Matthias Pospiech Matthias Pospiech - 2 months ago 14
C++ Question

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 row-major is, when the combined index increased by 1, the corresponding row index would be same (assuming not overflowing to the next row).

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.