Skip navigation

In C, when you define an array like:

int a[2][3];

DOES C consider that as 2 arrays of 3 ints, or is it 3 arrays of 2 ints?

I’ll tell you the answer soon .

.
.
.
.

But you have to guess first.

.
.
.
.
.
.

Ok.

IT IS TWO arrays of THREE ints. You read it from left to right.

So, if we define:

int a[2][3] = {
 { 3, 5, 7 },
 { 9, 1, 0 }
} ;

We know that’s right because if we tried to do it any other way, (like

 int a[2][3] = {
 { 9, 9 },
 { 9, 0 },
 { 2, 2 }
} ;

) we’d get something like “error: too many initializers” from the C compiler. That’s because this HAS TO BE an array of TWO arrays of THREE ints each.

ok ok but what about row major vs column major?

Row major vs column major really has to do with whether you think of a matrix as a collection of row vectors (“row major”), or if you prefer to think of a matrix as a collection of COLUMN VECTORS (aka “column major”).

Let’s start with column major.

interpreting the matrix as column major

So, don’t forget, we had:

int a[2][3] = {
 { 3, 5, 7 },
 { 9, 1, 0 }
} ;

If we’re thinking COLUMN MAJOR, then we will understand this to be 2 COLUMN VECTORS, WITH 3 ENTRIES EACH.

SO, we’d visualize the array as:

3 9
5 1
7 0

interpreting the matrix as ROW MAJOR

If we were to think of that same matrix:

int a[2][3] = {
 { 3, 5, 7 },
 { 9, 1, 0 }
} ;

If we’re thinking ROW MAJOR, then we will understand this to be 2 __ROW__ VECTORS, WITH 3 ENTRIES EACH.

So we’d visualize:

3 5 7
9 1 0

dealing with both types

Ok so notice the COLUMN MAJOR matrix was like:

3 9
5 1
7 0

and the ROW MAJOR was like:

3 5 7
9 1 0

So, let’s notice a few things that are interesting.

Notice that the original data was:

int a[2][3] = {
 { 3, 5, 7 },
 { 9, 1, 0 }
} ;

Now, notice this. The COLUMN MAJOR version has the values go inside it IN ORDER, DOWN THE COLUMNS.

3 9
5 1
7 0

The ROW MAJOR version has the values go inside it, IN ORDER, DOWN THE ROWS.

3 5 7
9 1 0

accessing and modifying values of row major and column major mats

If you’re accessing the COLUMN MAJOR matrix, you’d write something like:

a[1][2] = 99;  // set a[COL=1][ROW=2] TO 99;
// In the COLUMN MAJOR MATRIX,
// the specification of the COLUMN to hit
// comes first.
3 9
5 1
7 99

If you’re accessing the ROW MAJOR matrix, you’d write something like:

a[1][2] = 2000;  // set a[ROW=1][COL=2] TO 2000;
// In the ROW MAJOR matrix,
// specification of the ROW to hit
// comes first.
3 5 7
9 1 2000

BUT NOTICE, in the two green examples, we are HITTING THE SAME ENTRY IN THE MATRIX.

accessing a 1D array as row major, or column major

SAY you stored the matrix values in a 1D array.

int m[6] = {3,5,7,9,1,0};

Results in something like:

3 5 7 9 1 0

in C memory.

If you want to treat this 1D array as a COLUMN MAJOR matrix, you’d access it like this:

A[col][row] = A[row + col*NUMROWS];

If you want to treat this 1D array as a ROW MAJOR matrix, you’d access it like this:

A[row][col] = A[col + row*NUMCOLS];

About these ads

One Comment

    • Saad
    • Posted April 28, 2014 at 5:42 am
    • Permalink

    Thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 41 other followers

%d bloggers like this: