Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Representing matrix using 1-dimensional array
04-06-2011, 12:18 AM
Post: #1
Representing matrix using 1-dimensional array
I think this is a well known way of representing a matrix (2-dimensional array) using 1- dimensional array, but since I've found out about it just yesterday, I decided to post it.

Lets say we have a matrix which dimension is m x n, where m is number of rows, and n is number of columns. Length of the array in which we want to store our matrix would be m*n.

[Image: Dwh.jpg]

This is how we calculate the position (index) of the number in 1-dimensional array if we have the i and j which represent row and column values for number in matrix a[m][n].

[Image: a0U.jpg]

The reverse would be getting position in matrix if you have the index in 1-dimensional array.

[Image: k3o.jpg]

Thats about it.
Thanks myph.us for being so convenient.

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
04-06-2011, 12:33 AM (This post was last modified: 04-06-2011 12:34 AM by l3g1sl4tor.)
Post: #2
RE: Representing matrix using 1-dimensional array
Yeah, very interesting subject... Very often we need to convert our data shapes from matrix to arrays, so this will be very useful for every1 in here Smile keep it up Smile Part of this story is similar to pagination system commonly used in PHP and web design Smile

Read rules Smile
[Image: legislator.png]
Find all posts by this user
Quote this message in a reply
04-06-2011, 05:57 AM
Post: #3
RE: Representing matrix using 1-dimensional array
Yeah, that is good example of data structures !
As we can see memory is linear (one dimension) and we need to represent 2-dimensional (as matrix) object as one-dimensional.
BTW Here is explained row-major order of storing matrix (or any multi-dimensional array) in memory.
Row-major order is used in C,C++,Pascal etc.
And other method is column-major order, it is used in Fortran, Matlab etc.
Alphablend made good point and you can easily get address-function from his example !
Also when you dynamically allocate matrix (row by row) than you might not get this type of matrix representation !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: