Row-major vs Column-major confusion

后端 未结 9 1585
离开以前
离开以前 2021-01-31 19:27

I\'ve been reading a lot about this, the more I read the more confused I get.

My understanding: In row-major rows are stored contiguously in memory, in column-major colu

9条回答
  •  没有蜡笔的小新
    2021-01-31 19:43

    Doesn't matter what you use: just be consistent!

    Row major or column major is just a convention. Doesn't matter. C uses row major, Fortran uses column. Both work. Use what's standard in your programming language/environment.

    Mismatching the two will !@#$ stuff up

    If you use row major addressing on a matrix stored in colum major, you can get the wrong element, read past end of the array, etc...

    Row major: A(i,j) element is at A[j + i * n_columns];  <---- mixing these up will
    Col major: A(i,j) element is at A[i + j * n_rows];     <---- make your code fubar
    

    It's incorrect to say code to do matrix multiplication is the same for row major and column major

    (Of course the math of matrix multiplication is the same.) Imagine you have two arrays in memory:

    X = [x1, x2, x3, x4]    Y = [y1, y2, y3, y4]
    

    If matrices are stored in column major then X, Y, and X*Y are:

    IF COL MAJOR: [x1, x3  *  [y1, y3    =   [x1y1+x3y2, x1y3+x3y4
                   x2, x4]     y2, y4]        x2y1+x4y2, x2y3+x4y4]
    

    If matrices are stored in row major then X, Y, and X*Y are:

    IF ROW MAJOR:  [x1, x2    [y1, y2     = [x1y1+x2y3, x1y2+x2y4;
                    x3, x4]    y3, y4]       x3y1+x4y3, x3y2+x4y4];
    
    X*Y in memory if COL major   [x1y1+x3y2, x2y1+x4y2, x1y3+x3y4, x2y3+x4y4]
                  if ROW major   [x1y1+x2y3, x1y2+x2y4, x3y1+x4y3, x3y2+x4y4]
    

    There's nothing deep going on here. It's just two different conventions. It's like measuring in miles or kilometers. Either works, you just can't flip back and forth between the two without converting!

提交回复
热议问题