Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Three-Dimensional Arrays

I'm trying to wrap my head around three-dimensional arrays. I understand that they are arrays of two-dimensional arrays, but the book I'm reading said something that confuses me.

In an exercise for the book I'm reading, it asks me to make a three-dimensional array for a full-color image. It gives a small example saying this:

If we decide to choose a three-dimensional array, here's how the array might be declared:

int[][][] colorImage = new int[numRows][numColumns][3];

However, wouldn't it be more effective like this?

int[][][] colorImage = new int[3][numRows][numColumns];

Where 3 is the rgb values, 0 being red, 1 being green, and 2 being blue. With the latter, each two-dimensional array would be storing the color value of the row and column, right? I just want to make sure I understand how to effectively use a three-dimensional array.

Any help will be greatly appreciated, thanks.

like image 426
Undefined Avatar asked Jul 24 '12 04:07

Undefined


People also ask

What are 3 dimensional arrays used for?

A 3D array provides range, azimuth and elevation information and represents a maximum complexity design. As the 2D array provides range and azimuth information only, it represents a medium complexity design. The arrays can be used for radar applications such as air-traffic control and surveillance.

What do you mean by 3D array?

3-D arrays are referred to as multi-dimensional arrays. Multi-dimensional arrays are defined as an “array of arrays” that store data in a tabular form. Imagine this, an array list of data elements makes a 1-D (one-dimensional) array. An array of 1-D arrays makes a 2-D (two-dimensional) array.

What is 3D matrix explain with example?

3-D Matrix is a multidimensional array that is an extension of two-dimensional matrices. As you can guess, they will have 3 subscripts, one subscript along with row and column indexes as for the 2D matrix. The third subscript in a 3D Matrix is used to represent the sheets or pages of an element.

Is 3 dimensional array possible?

You can think the array as a table with 3 rows and each row has 4 columns. Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3];


2 Answers

Order doesn't matter, and in fact the former form is more readable:

final const int RED = 0;
final const int GREEN = 1;
final const int BLUE = 2;

int[][][] colorImage = new int[numRows][numColumns][3];
//...

int x = getSomeX();
int y = getSomeY();

int redComponent = colorImage[x][y][RED];
int greenComponent = colorImage[x][y][GREEN];
int blueComponent = colorImage[x][y][BLUE];
like image 198
Strelok Avatar answered Oct 11 '22 17:10

Strelok


The order shouldn't matter, so one isn't more effective than the other. The only thing that matters is that whatever accesses colorImage knows which dimension is used for what. Bit more context on multidimensional arrays here.

like image 36
Tyson Avatar answered Oct 11 '22 17:10

Tyson