Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

element address in 3 dimensional array

I am looking for the formulas to find the memory location of an element in a 3-D Array for row major and for column major. After using my logic I end up with the following formulas. say array is A[L][M][N].

row-major:Loc(A[i][j][k])=base+w(M*N(i-x)+N*(j-y)+(k-z))
column-major:Loc(A[i][j][k])=base+w(M*n(i-x)+M*(k-z)+(j-y))

where x, y, z are lower bounds of 1st(L) 2nd(M) and 3rd(N) index. I tried this formula and got the correct result but when I applied this formula on a Question in the book then the answer did not match. Please can anyone help me out with this.

like image 217
Rohit Negi Avatar asked Nov 01 '15 15:11

Rohit Negi


People also ask

How do you find the address of an element in an array?

In a single dimensional array the address of an element of an array say A[i] is calculated using the following formula Address of A[i]=B+W∗(i–LB) where B is the base address of the array, W is the size of each element in bytes, i is the subscript of an element whose address is to be found and LB is the Lower limit / ...

How many elements are in a 3-dimensional array?

Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3]; Here, the array y can hold 24 elements.

How do you access elements in a 3D array?

Accessing elements in the 3D array is similar to any other array, by using the index of the element. We have to use three loops to access all the elements inside the array x[2][1][0]. For higher dimension arrays like 4, 5, 6, etc., the concept is quite similar, but the complexity of handling the things increases.


2 Answers

Formula for 3D Array

Row Major Order:

Address of

A[I, J, K] = B + W * [(D - Do)*RC + (I - Ro)*C + (J - Co)]

Column Major Order:

Address of

A[I, J, K] = B + W * [(D - Do)*RC + (I - Ro) + (J - Co)*R]

Where:

  • B = Base Address (start address)
  • W = Weight (storage size of one element stored in the array)
  • R = Row (total number of rows)
  • C = Column (total number of columns)
  • D = Width (total number of cells depth-wise)
  • Ro = Lower Bound of Row
  • Co = Lower Bound of Column
  • Do = Lower Bound of Width
like image 101
DorkCodingsign Avatar answered Sep 20 '22 23:09

DorkCodingsign


Right one is:

row-major:Loc(A[i][j][k])=base+w(N*(i-x)+(j-y)+M*N(k-z))
column-major:Loc(A[i][j][k])=base+w((i-x)+M*N(k-z)+M*(j-y))
like image 42
Manish Sharma Avatar answered Sep 22 '22 23:09

Manish Sharma