473,761 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confusion about row-major and column-major

I'm working on a project where i need to exchange multidimensiona l 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 4758
"Jef Driesen" <je********@hot mail.com.nospam > schrieb im Newsbeitrag
news:dq******** **@ikaria.belne t.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********@hot mail.com.nospam > schrieb im Newsbeitrag
news:dq******** **@ikaria.belne t.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,pl ane).
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
1260
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 managed to do this by going thru each row and creating an oraclecommand, passing the parameters and executenonquery, and it works reasonablly well. However instead of doing all that looping thru rows, I believe I can use the dataadapter UPDATE...
2
13893
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 the example that says: import csv writer = csv.writer(open("some.csv", "wb")) writer.writerows(someiterable)
16
3765
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 discovered the TableRowCollection this evening, so I was hoping that I could return one of these objects with my three rows inside it, and then assign the collection to the table...something like...
2
9966
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 data into it. I bound the DataTable to the DataGrid. The example at the end of this post is exactly as copied from the vb.net help screen: I put the new myDataGrid Class following the Form1 Class.
1
3180
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 between projects in the solution were established through project references, not by browsing to an assembly DLL. All of the projects are strongly named and use key files. Each project's AssemblyInfo.cs specifies the assembly version, where the...
5
1515
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, though, when I right click to set the primary key, the key icon shows up, but it doesnt' update the 'IDENTITY SPECIFICAION' below in the properties to YES like the other table. Why is that?
2
4668
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 retrieve the values for each of the "nodes". Running the debugger, I can see that I am navigating to each of the nodes, but when I look at the "Value" property, it is empty. I'm not sure what I am doing wrong. Here is a snipit of my XML file...
2
1015
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 not self.dict.has_key(rowName): self.dict = tableRow
4
3137
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 2000). A colleague has said that the primary key should be the clustered index because all index lookups will go via the primary key. Is this right? I thought the primary key was nothing more than a constraint on what data can be entered into the...
0
1658
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 ItemTemplate with Literal controls for Name, ID and Status and a DataList control for MoreData. DataList2 is using MoreData where fields are , and .
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3913
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 we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.