"Jef Driesen" <je********@hotmail.com.nospam> schrieb im Newsbeitrag

news:dq**********@ikaria.belnet.be...

C/C++ (row-major):

1D: [c] --> row vector

2D: [r][c]

3D: [p][r][c]

Matlab (column-major):

1D: (r) --> column vector

2D: (r,c)

3D: (r,c,p)

As you can see in 3D, the order of the dimensions is not what I expected.

r and c are not in reverse order. And the more I start reasoning about it,

the more I get confused. What am I missing?

You start with two different things - a row vector in C++ and a column

vector in Matlab. I don't know what Matlab does, but in C++ you get

int A1[n1];

one array with n1 elements

int A2[n2][n1];

n2 arrays, where each element is an array with n1 elements

int A3[n3][n2][n1];

n3 arrays, where each element is an array with n2 elements, where each

element is an array with n1 elements.

You can call A1 a row vector or a column vector. C++ doesn't care, but it

influences the use of 2D or 3D arrays. If you call A1 a row vector, the last

subscript refers to a column, the second to last to a row, and the third to

last to a plane. (I don't have names for other dimensions, so I better stop

here.) Now if you call A1 a column vector, the last subscript selects its

row, the second to last its column, and the third to last again selects a

plane. Using your notation, in the second case you get

1D: [r]

2D: [c][r]

3D: [p][c][r]

What you have to be carefull with, is the mapping to linear memory. For a 1D

array it is easy. In memory you have A1[0], A1[1], ... A1[n1-1]. For a 2D

array you get A2[0][0], A2[0][1], ... A2[0][n1-1], A2[1][...], ...

A2[n2-1][n1-1].

For a 3D array you'll find A3[i3][i2][i1] at offset (i3 * n2 + i2) * n1 + i1

in linear memory (ignoring the size of individual elements).

HTH

Heinz