Representing matrix using 1dimensional array

04062011, 12:18 AM
Post: #1




Representing matrix using 1dimensional array
I think this is a well known way of representing a matrix (2dimensional 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. This is how we calculate the position (index) of the number in 1dimensional array if we have the i and j which represent row and column values for number in matrix a[m][n]. The reverse would be getting position in matrix if you have the index in 1dimensional array. Thats about it. Thanks myph.us for being so convenient. 

04062011, 12:33 AM
(This post was last modified: 04062011 12:34 AM by l3g1sl4tor.)
Post: #2




RE: Representing matrix using 1dimensional 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 keep it up Part of this story is similar to pagination system commonly used in PHP and web design
Read rules 

04062011, 05:57 AM
Post: #3




RE: Representing matrix using 1dimensional array
Yeah, that is good example of data structures !
As we can see memory is linear (one dimension) and we need to represent 2dimensional (as matrix) object as onedimensional. BTW Here is explained rowmajor order of storing matrix (or any multidimensional array) in memory. Rowmajor order is used in C,C++,Pascal etc. And other method is columnmajor order, it is used in Fortran, Matlab etc. Alphablend made good point and you can easily get addressfunction 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 

« Next Oldest  Next Newest »
