473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Confusion about row-major and column-major

I'm working on a project where i need to exchange multidimensional data
between C/C++ (row-major) and matlab (column-major). I understand the
difference between those two mappings to linear memory.

Suppose I need an S1 x S2 x ... x Sn dimensional array A. I can have the
same layout in memory by reversing the dimensions:

A[S1]...[Sn] == A(Sn,...,S1)

where I used the C/C++ notation for row-major and the matlab notation
for column-major (and forget about the issue of zero-based vs one-based
access). So far no problem.

But when I start "naming" the dimensions (with row and column), this
reversing does not seem to hold anymore. Let my explain that by
progressively adding dimensions in both cases: (r=row,c=column,p=plane)

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?
Jan 12 '06 #1
2 4732
"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
Jan 12 '06 #2
Heinz Ozwirk wrote:
"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


I started with a row vector in C++ and a column vector in Matlab,
because that seemed to me the most "natural" way, associated with each
memory layout.

The matlab notation for a 3D array is A(row,column,plane).
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.
As long as you don't start naming the dimensions, everything is fine.
This is basically the structure (I hope my ascii drawing does not get
destroyed):

A3
---
| | -> A2
--- ---
| | | | -> A1
--- --- ---
| | | | | |
--- --- ---
| | | |
--- ---
| |
---
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]
For the 3D case this is perfectly consistent with idea of reversing the
dimensions. But now the last dimensions (index [r], which is actually a
column) is stored continuously in memory. And this contradicts with the
term row-major.
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).


Mapping an element to linear memory is not too difficult because it does
not depend on my naming issue.
Jan 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul | last post by:
OK I'm lost.... Hope I can describe this that you will understand what I'm taking about. I have an Oracle dataset and I've inserted some rows into it, I now want to update the database. I...
2
by: googleboy | last post by:
Hi. I am trying to write out a csv file with | instead of comma, because I have a field that may have many commas in it. I read in a csv file, sort it, and want to write it out again. I read...
16
by: Rob Meade | last post by:
Hi all, I am just making a few methods in my base class, one of which returns a horizontal line, formatted on the page as *I* want it..its basically a collection of three table rows. I...
2
by: Ron Dahl | last post by:
I'm very confused on how the Protected Overrides works. I created a new project with a new form1 and a new datagrid called myDataGrid. I created a simple DataTable and put 5 rows and 5 columns of...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
5
by: darrel | last post by:
I have two tables in my SQL Express DB within my application. On one table, I can right-click on the row and set it to be the primary key. And it is the primary key. On the other table,...
2
by: =?Utf-8?B?SmltSGVhdmV5?= | last post by:
Working with my first XML file and the first time I have used the XmlTextReader class. Reading a book on this subject, I have constructed code which I thought would navigate thru the file and...
2
by: aine_canby | last post by:
Why are the following different? def AddRow(self, rowName, tableRow=TableRow(ReleaseDate(""), ExpiryDate(""))): # check to see if the row already exists, if not add it to the container if...
4
by: codefragment | last post by:
Hi I thought that given a table with an index, primary key and clustered index any non clustered index look ups would go via the clustered index and the primary key is irrelevant? (sql server...
0
by: Brian Lowe | last post by:
I'm in a web page and I have hierarchical data so I'm using 2 nested DataList controls. DataList1 is using a data source with rows of , , and where MoreData is a list. DataList1 has an...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.