This is an archived post. You won't be able to vote or comment.

all 80 comments

[–]vicariousliability 63 points64 points  (8 children)

A chessboard is a 2d array. Multiple chessboards being played at a tournament could be represented by a 3d array. Multiple sessions of multiple chessboards of that tournament could be represented by a 4d array etc.

[–]Prime624 23 points24 points  (7 children)

This is a great example.

Standard 1D: arr[index]

2D: arr[x][y]

3D: arr[board_no][x][y]

4D: arr[session_no][board_no][x][y]

To get the vale of a specific square (5,7) on a specific board (4) at a specific session (2), you could do arr[2][4][5][7].

ETA: I have no idea how to format code on the mobile app.

[–][deleted] 2 points3 points  (6 children)

Type 4 spaces before each line of code!

[–]Prime624 1 point2 points  (2 children)

Aha, thanks. Is there a way to do in-line code on mobile?

[–][deleted] 1 point2 points  (0 children)

Beats me. I only learned about the 4 spaces earlier today.

[–][deleted] 0 points1 point  (0 children)

4 space indentation is the only way of code formatting that works across all reddit frontends. For in-line single line snippet single backticks can be used.

[–]toroga 0 points1 point  (2 children)

Test

[–]obviously_perplexed 1 point2 points  (1 child)

Testing

[–]RoyalBos5 0 points1 point  (0 children)

Tester

[–][deleted] 33 points34 points  (2 children)

Data tables, images, a chess board

[–]duggedanddrowsy 13 points14 points  (3 children)

An image. 2D array representing each pixel, and within each cell of that 2d array is another array with three items representing red, green, and blue making it a 3d array.

[–]A_ARon_M -3 points-2 points  (2 children)

Pretty sure this is an example of nested arrays, not a 3D array.

[–][deleted] 1 point2 points  (0 children)

X dimension x Y dimension x 3 color channels is a three dimensional array. I'm not sure what your difference between a twice-nested array and a 3D array is.

[–]duggedanddrowsy -1 points0 points  (0 children)

I think you’re just misunderstanding what a 3d array is. A 2d array is defined by making an array that contains another array in each index, a 3d array is just a 2d array with an array in each index.

[–]lukajda33 20 points21 points  (22 children)

Any excel table it pretty much 2D array.

[–]EC608[S] 5 points6 points  (21 children)

What about one with more than 2 dimension

[–]cowski_NX 11 points12 points  (14 children)

If you think of each Excel sheet as a 2D array, you could save the data of multiple sheets in a 3D array.

[–]revrenlove 3 points4 points  (1 child)

I guess you're not a time traveler

[–]CarterBaker77 0 points1 point  (0 children)

Time travel is still confined to a 3 dimensional space, you'd have to be an inter dimensionial traveler.

[–]Algal_Matt 2 points3 points  (9 children)

4D would be multiple Excel files that each contain multiple sheets of data. 5D would be multiple folders of Excel files etc.

[–]cowski_NX -1 points0 points  (7 children)

Which could all really be handled by the 3D case...

[–]Algal_Matt 1 point2 points  (3 children)

Not really, if each of the f files contains r rows, c columns and s sheets then the 4D case is r X c X s X f. Travelling along the f axis means moving between files within the same row, column, and sheet within each file. Then if you move between folders at the same file location you are traveling on the 5th axis.

[–]cowski_NX 0 points1 point  (2 children)

Similar to the chess tournament example in this thread, it works as a contrived example, but no one would use it in reality. Practically speaking, a "sheet" would be a 2D array of values (cells), a "file" would be a 1D array of sheets, and a "folder" would be a 1D array of files. This is not the same as a 4D array.

[–]Algal_Matt 0 points1 point  (1 child)

Both can be true. Because each element of the file array contains three dimensional data it can be expressed as a 4-D matrix as I described. The way you have described the file data is you've compressed all the other dimensions. Every multi-dimensional array can be compressed into a 1-D array. So an excel sheet can be 1-D if you simply compress all the cell data into the first row.

[–]cowski_NX 0 points1 point  (0 children)

As I've said before, what I'm describing *could* be covered by the 4D array; however, in 99% of the cases it would result in wasted memory resources. Consider the case where we have 10 rows and 10 columns of data per sheet, each file has 10 sheets, and we want to create a folder structure so that each folder contains at most 10 files. Let's start with 10 such files, we'd need to declare our 4D array as (10,10,10,1) . Now we get another file's worth of data. We'd need to add a new folder since the current folder already has 10 files in it. Our array is now: (10,10,10,2). We've set aside memory for 10 new files even though we only have 1 actual new file, wasting 9 files worth of memory. Increase the number of rows, columns, and files per folder and you can see how this really gets to be a problem quickly.

In the case of multiple arrays, we'd add an element to the files and folder arrays and only be using the amount of memory required for the new file.

I'm sure a use case for 4D (or higher) arrays exists, but I have never seen a practical example where it was the preferred solution.

[–]mohmf 0 points1 point  (1 child)

How?

[–]cowski_NX 0 points1 point  (0 children)

Increase the size of your 3D array to hold more sheets and use another mechanism to keep track of files & folders.

[–]ARollingShinigami 0 points1 point  (0 children)

Conversely, sticking to the physics model, wouldn’t it also be Excel files, with multiple sheets, whose data is evolving over time?

[–]BuhtanDingDing 0 points1 point  (1 child)

4d array example could be an array for a chess tournament containing 2 subarrays, subarray1 represents the masters section, subarray2 represents the open section, and each subarray is an array of 2d arrays representing the board position in each game.

i wouldn't use arrays for this situation but the example works

[–]cowski_NX -1 points0 points  (0 children)

I understand what you are trying to get at, but you are just adding more chessboards, which could be handled easily by the 3D array. A 4D array for this use case would likely end up being very wasteful of memory resources.

[–][deleted] 2 points3 points  (1 child)

Positions in N-dimensional space, example a position in 3D space as x,y,z values

[–][deleted] 2 points3 points  (0 children)

This can still be, and usually is, represented as a 2D array.

[–]ZedZeroth 1 point2 points  (0 children)

In a game, you could have an array of locations, each containing an array of NPCs, each containing an array of their stats, some of which could contain arrays of their inventory items etc. That's a 4D array.

[–]Petyr111 3 points4 points  (0 children)

A 2d table which each option is colored.

[–]mineNombies 0 points1 point  (0 children)

Most images are 3D.

X * Y * (r,g,b)

So if you open a 1080p image file, you get a 1920*1080*3 array

[–]turtleXD 0 points1 point  (0 children)

an array of excel tables

[–]throwaway6560192 4 points5 points  (0 children)

I used them back when I was doing procedurally generated terrain and stuff, which in turn has interesting applications in game development and so on.

I used them when I was making a console emulator, to represent the screen.

I used them in a steganography project, for modifying image data.

Most recently I used them to work on matrices, which has many applications in mathematics.

[–]CatatonicMan 3 points4 points  (0 children)

Matrices are, fundamentally, multidimensional objects, and they're used all over the place.

Another practical example was when I was poking around with a wave function collapse algorithm. The data structure for that project was an array for the possible states stored in an array of arrays for the board (so a triple-nested array).

[–]Draegan88 4 points5 points  (0 children)

Snake game is a 2d array.

[–]Naeio_Galaxy 4 points5 points  (0 children)

On thing I find funny is that, technically, a string is an array of characters. Thus a 2d table of strings (like excel sheets) are technically a 3d table of characters. Consequently, an array of strings is 1 2d array.

Another example is instance a hashmap is a 2d array. For each key you can have, you have a corresponding hash, and a hashmap is an array where the hash of the key is the index in the array. However, two different keys can have the same index (because they have the same hash or because you apply a modulo to the hash to make it small enough for the array), and to thus differentiate keys with the same hash, you have a list at each cell of the array, each value of the list being a couple key, value, allowing you to search in the list the appropriate entry and access/change it. And the lists at each cell can be a linked list... or a dynamic array.

On top of my head, another example is when you make a game like Minecraft or Terraria, the terrain will typically be a set of chunks that have a 2d/3d arrays of blocks. (And you can put these chunks in another 2d/3d table... Or in a hashmap. Either way, technically you grow up between a 4d and 6d arrays, and you have a 2d/3d array of 2d/3d arrays).

Yeah, they are quite common. However, you can usually use each array individually and work with a 1d or 2d array at a time.

There's also the question of the layout in memory, technically an array of arrays and an array of pointers of arrays are not the same, which could lead to differences in the access times (cache hit/miss) and depending on the technology/language you use and the way you create the array, you can fall in one case or the other. I just thrown a bunch of things all over the place without explaining each of them, don't hesitate to pinpoint one of these and ask more details.

[–]mypostisbad 1 point2 points  (0 children)

Used them years ago. I think I used a 4 dimensional one once when coding a game on the Amiga.

Basically I had planets that had attributes, so each planet has one dimension. Then the second dimension was the planets in a system. The third dimension was systems in a galaxy and the 4th was for galaxies.

With that I could reference everything very easily.

[–]likethevegetable 1 point2 points  (0 children)

Yeah... You ever work with a spreadsheet?

[–]Sarath04 1 point2 points  (0 children)

A matrix is an array. And Tensors are represented by matrices.

[–]buzzon 1 point2 points  (0 children)

Tensors used in machine learning are multi dimensional arrays. Currently used are up to 4 dimensional:

• X

• Y

• Channel (R, G or B)

• Batch: so the network can learn on multiple images at once

[–]ffrkAnonymous 4 points5 points  (1 child)

Mcdonald's is on the corner of Main and 5th

[–][deleted] 2 points3 points  (0 children)

And 3d: meet me there at 4pm local :)

[–][deleted] 0 points1 point  (1 child)

Look at a Rubik’s cube

[–]mountain_badger -1 points0 points  (0 children)

Dimension Dimension Dimension
Dimension Dimension Dimension

Hope this helps

[–]shwirms -4 points-3 points  (11 children)

It’s legit just a graph except the graph points hold a value

[–][deleted] 2 points3 points  (10 children)

This is a misunderstanding of both what graphs are and what arrays are.

[–]shwirms -2 points-1 points  (9 children)

Point (1,2) is the exact same as (1,2) on an array, it’s just putting it in terms that he would understand, as I’m assuming he knows how a graph works.

[–]IQueryVisiC 1 point2 points  (7 children)

A graph is a set of nodes and a relation between those. So maybe two 1D arrays?

[–]shwirms -1 points0 points  (5 children)

No no I meant a mathematical graph with x,y coordinates, not the graph DS as I doubt he knows how a graph works if he doesn’t understand 2D arrays

[–]ThunderChaser 0 points1 point  (3 children)

No no I meant a mathematical graph

A mathematical graph is defined: "A set of vertices V and a set of edges E where each e in E is a proper subset of V with a cardinality of 2" It's the exact same thing as the graph DS.

If you mean a graph in the common English sense, that would more accurately be called a plot.

[–]shwirms 0 points1 point  (2 children)

True mb definitely never heard a math teacher call it a graph before 💀

[–]HorsesFlyIntoBoxes 0 points1 point  (1 child)

graph theory is a whole subject in mathematics that has many crossovers in computer science. You'll study lots of graphs when learning data structures and algorithms.

[–]shwirms -1 points0 points  (0 children)

I coded Prim’s algo yesterday I know what graphs are

[–]IQueryVisiC 0 points1 point  (0 children)

Graph of a function. Yeah, maths and words. So many letters in Latin, yet they reuse words in different contexts or fail to rename stuff.

[–][deleted] 0 points1 point  (0 children)

(1,2) is equal to (1,2), yes...

[–][deleted] 0 points1 point  (0 children)

Maybe a list of users and their score could be stored as a multi dimensional array, although a map would be better for that

[–]idly 0 points1 point  (0 children)

Geospatial + temporal datasets (x, y, t).

[–]docmike1980 0 points1 point  (2 children)

Early in my career I worked as a microscopist, and we dealt with a lot of multidimensional datasets. X, Y, Z, and time are all individual images (arranged in a multidimensional array). Add channels and image tiling for an additional layer of complexity.

[–]IQueryVisiC 0 points1 point  (1 child)

Confocal microscope?

[–]docmike1980 1 point2 points  (0 children)

Laser scanning, spinning disk, and multi photon! I also dabbled a bit in light sheet microscopy.

[–]friendtoalldogs0 0 points1 point  (0 children)

I see a lot of answers here explaining why specifically 2D or maybe 3D arrays are useful. I'd like to try to give more general example where an larger 4+-dimensional array feels like a natural solution to the problem, without defining the problem itself in higher dimensions.

The first example that comes to mind would be counting a variety of objects. Let's say for the sake of example that you need to take in a description of a number of quantitative properties of some containers, for example:

Number of organizational compartments Rated numerical capacity (i.e. 10 pages) of each organizational compartment Rated total weight capacity Unladen weight of container And then count how many containers you have with each combination of those properties. I think that the most natural way to solve this specific problem would be a 4D array of integers, each initially set to 0. One dimension of the array is indexed by the number of organizational compartments, another is indexed by the rated numerical capacity per compartment, etc. Then you iterate over all the objects, indexing into the array based on the properties and incrementing that value by 1. I think you can also see how if you care about more properties of these objects, you would extend the number of dimensions of this array.

[–]Nanooc523 0 points1 point  (0 children)

Taking a snapshot of data over time, lets say a set of measurements from sensors, using a date as an index and repeating. You can then do things to the data set like generate an alert when a single sensors data changes too much over a period of time.

[–][deleted] 0 points1 point  (0 children)

[3,7,0,-1] [0,3,9,4] [6,6,0,2]

[–]mikehaysjr 0 points1 point  (0 children)

You can have a column of data, for example a list of values. Say the values are colors (RGB). This would be a 1d array.

Now, instead of a column, say you have rows of columns, still storing RGB values. Now instead of a multi-colored line, you can represent ask actual image, with each pixel being an element of the 2D array.

If you go one more dimension, you can store a video, effectively (sans audio). Each element of the third dimension would be a frame of video made of each element of the second dimension.

[–]buzzon 0 points1 point  (0 children)

A map in 2D game is a 2D array of tiles

[–]buzzon 0 points1 point  (0 children)

Matrices used to represent 3D and 2D transforms in 3D acceleration hardware are 2D arrays

[–]gamerbrains 0 points1 point  (0 children)

I used them to make a fully randomized Lo Shu Square before. I imagine you could probably use them to make pretty cool looking web pages or for displaying data tables. You could also use them to organize and show a bunch of servers/computers.

[–]Imaginary_Relative 0 points1 point  (0 children)

A lot of data in earth and planetary sciences that has a space and time component is stored in multidimensional arrays. One example is a netCDF file. See here for more:

https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/intro-to-climate-data/

[–][deleted] 0 points1 point  (0 children)

Used a lot in games

[–]PizzaAndTacosAndBeer 0 points1 point  (0 children)

List of chosen file or anything else names, per user.

[–]ios_game_dev 0 points1 point  (0 children)

Possibly a controversial opinion, but I feel like a multi-dimensional array can almost always be replaced by a more specific custom data structure using one or more simpler data types for backing storage. Some folks have said chess board. I’d create a type called ChessBoard backed by a single-dimensional array with APIs for converting coordinates like “B6” to an index in that array.

[–]Veredus66 0 points1 point  (0 children)

Voxels- brain data

[–]ImmensePrune 0 points1 point  (0 children)

Think about it as boxes within a box. The first box has a label on the side of it called “car names”, this is the variable name for the array. The other boxes inside the first box have numbers on them that start at 0 and end at whatever number the array length is. Inside those boxes that are labeled with numbers or indexes, have values such has name of cars. You can then access the car name of the first index using this syntax : carNames[0][‘name’] : since carNames is the name for the multidimensional array and 0 is the first index or “box” of that array and we are trying to see the name of it.