Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Abstract

So, as I understand it (although I have a very limited understanding), there are three dimensions that we (usually) work with:

The 1st could be represented by a line. The 2nd could be represented by a square. The 3rd could be represented by a cube.

Simple enough until we get to the 4th -- It is kinda hard to draw if you know what I mean... People say that it has something to do with time.

The Question

Now, that is all great with me. My question isn't about this, or I'd be asking it on MathSO or PhysicsSO. My question is: How does this work with arrays?

I know that you can create 4D, 5D, 6D, etc... arrays in many different programming languages, but I want to know how that works.

Anyone willing to enlighten me? :)

share|improve this question
3  
Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask –  gnat 14 hours ago
15  
If 3 dimensions can be visualized as a cube, then 4 dimensions can be visualized as a bunch of cubes in a line. 5 dimensions can be visualized as a grid where each cell in the grid contains... a cube! And so on... The "Fourth Dimension" has nothing to do with time (whatever that is), unless you define it as such in the context of the semantics of your program. –  FrustratedWithFormsDesigner 14 hours ago
6  
In general, you can get over this conceptual hump by trying to avoid thinking of dimensions as strictly physical constructs. For example, some machine learning problems can have a dimensionality in the hundreds of thousands, where each dimension is a feature of the dataset. –  Steve Evers 11 hours ago
3  
(1) Vector space (2) Multi-dimensional arrays –  rwong 11 hours ago
7  
Remember that the computer doesn't care about the idea of geometric dimensions--those are just a device for human convenience. If you allocate a 5x5x5x5 array, the computer just allocates an array of 625 elements and does math with your indices accordingly. –  David Zhang 9 hours ago
show 3 more comments

7 Answers

Fortunately, programs aren't limited by the physical constraints of the real world. Arrays aren't stored in physical space, so the number of dimensions of the array doesn't matter. They are flattened out into linear memory. For example, a single dimensional array with two elements might be laid out as:

(0) (1)

A 2x2 dimensional array might then be:

(0,0) (0,1) (1,0) (1,1)

A three dimensional 2x2x2 array might be:

(0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) (1,0,1) (1,1,0) (1,1,1)

You can hopefully see where this is going. Four dimensions might be:

(0,0,0,0) (0,0,0,1) (0,0,1,0) (0,0,1,1) (0,1,0,0) (0,1,0,1) (0,1,1,0) (0,1,1,1)
(1,0,0,0) (1,0,0,1) (1,0,1,0) (1,0,1,1) (1,1,0,0) (1,1,0,1) (1,1,1,0) (1,1,1,1)
share|improve this answer
3  
Computer arrays are not limited by human comprehension or visualization, but they're limited by physical constraints, e.g. an array of d dimensions each of length n will take n^d, or more generally with different length dimensions, n1 × n2 × … × nd. –  Paulo Madeira 13 hours ago
    
Greg, I was under the impression that the dimensions in arrays referred to their nesting. E.g. 1d (1,2), 2d (1, (A,B)), 3d (1, (A, ($, %))), etc. What is the correct term for this sort of phenomena? Nesting level 3? –  Colton Allen 12 hours ago
1  
@ColtonAllen: I'm not quite sure what you're referring to. The definition of dimension says "Roughly speaking, it is the number of coordinates needed to specify a point on the object." An array declared in C as int a[2][2][2]; would be a 3-dimensional array. –  Greg Hewgill 12 hours ago
    
@ColtonAllen: I'm not aware of any term for that. That just sort of begins to be conceptualized as a hierarchical data structure rather than an array. –  whatsisname 12 hours ago
    
@ColtonAllen: Could you ask that as an independent question? –  David Cary 9 hours ago
show 4 more comments

The dimensions are whatever you want to be, the 4th dimension doesn't necessarily have to be time. If you think of three dimensions as a cube, you can think of 4 dimensions as a row of cubes. 5 dimensions, a grid of cubes, and so on.

You could also have a 3d collection of voxels, with a 4th dimension being color, or density, or some other property.

When you allocate the memory for your multidimensional array, it just simply allocates the product of each dimensions maximum for your data type. If you have a 3d array or 'cube' of 10 elements in each dimension, you'll have 1,000 elements allocated. If you make that a 4d array with 10 elements in the 4th dimension, the computer will just allocate 10,000. Bump it up to 5 dimensions, and it will allocate 100,000.

The computer doesn't care about any sort of meaning about what each dimension represents. To select where in the list of elements a single point is, it's just multiplication to select a memory address.

share|improve this answer
add comment

An array is only a block of continous memory. Memory addressing is one-dimensional, you can either go forward or backward. So assuming you have an array with 5 elements, 5 memory blocks will be reserved. If you have a 2-dimensional array with 5 elements in each dimension, 25 memory blocks will be reserved.

share|improve this answer
2  
Yes, keep this in mind as you add more dimensions to your arrays. Memory usage may not be trivial. –  davidhaskins 14 hours ago
    
@davidhaskins You could make each dimension have size 1 and get away with a very low memory footprint ;) sizeof(char[1][1][1][1][1][1][1][1][1][1]) == 1 –  FredOverflow 2 hours ago
add comment

Imagine doing R&D on some new medical device, a series of sensors that you put along a patient's arms. You have seven volunteers lined up for testing. Each sensor reports low-frequency, mid-frequency, and high-frequency readings, which you take once every 100ms for about a minute.

How to store all that data in memory for analysis and plotting?

An array, obviously. It would look like this (using made-up generic pseudocode):

npatients = 7
nsensors = 4     // number of sensors on an arm
nchannels = 3
nsamples = 60.0 / 0.1
sensordata = Array[ npatients, nsensors, 2, nchannels, nsamples ]

That's a five dimensional array, and there's nothing tricky, mysterious or baffling about it. There is no reason to try to associate it with 5-dimensional Euclidean space. To obtain any one data value, we use an expression like

x = sensordata[6, 5, 1, 2, 338)

It just like querying a relational database where you have a record for each data value, with five columns holding patient id, sensor id and so on, and a column with the value. To get one data point you use five terms in the WHERE: SELECT value FROM SensorData WHERE (patientid=6) and (sensorid=5) and (arm="left") and (channel="midfreq") and (sampleindex=338).

There is nothing mystical about a database table with five or more columns, is there?

(I'm using 1-based indexing though in real life, 0-based is much more common.)

Note that I'm a bad boy due to hard-coding the number of arms. If I'm ever given funding to investigate these sensors on an octopus, I'm in trouble!

share|improve this answer
    
+1 Excellent example demonstrating that dimensions can be any data that you require. –  Mike 10 hours ago
add comment

...or I'd be asking it on MathSO...

Well, as a matter of fact mathematicians would never (or at least not usually) associate a fourth dimension with anything like time. Nor would they associate the first three ones with anything space like: mathematicians simply define dimension as an abstract property of, typically, a vector space (often this will be generalised to manifolds or even metric spaces). And this abstract definition doesn't care about how many dimensions the physical space we happen to move in has. The concept of dimensions applies to spaces that don't even resemble the physical space. In fact mathematicians (and indeed physicists) very often use infinite-dimensional spaces, such as the Hilbert spaces of quantum mechanics.

With that clarified, let's talk arrays – you don't need to understand vector spaces, since the abstract definition is actually much simpler here.

An (0 × 1 × 2 × ... × n−1)-sized array (i.e. of dimension n) is simply a collection of 01 ⋅ ... ⋅ n−1 numbers (or whatever type of object populates the array). The only difference to a one-dimensional array of that length is that you have a particular useful way of indexing the dimensions seperately, namely

ilin = in−1 + n−1 ⋅ (in−2 + n−1 ⋅ ( ... 2 ⋅ (i1 + 1i0)...))

share|improve this answer
add comment

You don't need to imagine in high spatial dimensions, just think of it as a fern leaf. fern leaf

The main stalk is your first array dimension, each main branch second dimension, the third dimension is the little branches off that.

You can also look at it the other way. The actual leafs are the data in the innermost data of the highest dimension array. These are joined together into a branch, then the larger branches are made up of smaller branches until you get to the top level branch. This is the same as the arrays holding arrays building up back to the first array.

share|improve this answer
add comment

In programming, arrays are quite easy to implement, but maybe not to understand.

Generally, each level of arrays means to have the content n-fold. That means

  • int x[4] are 4 blocks, each of them containing an int.
  • int x[5][4] are 5 blocks, each of them containing an int[4].
  • int x[3][5][4] are 3 blocks, each of them containing an int[5][4].
  • int x[2][3][5][4] are 2 blocks, each of them containing an int[3][5][4].

How you are referring to them is up to you, but for better understanding, you have something like

  • COLUMN for the last one
  • ROW for the second-last one
  • PAGE for the third-last one

Till here, I read it somewhere. In order to stay here, we can as well define

  • BOOK for the fourth-last one
  • and maybe SHELF for the fifth-last one. (Or, if you prefer, SHELFROW so that we can continue.)

That said, I never saw array with more than 4 or maybe 5 dimensions in "wild life".

This way, you can define and imagine int x[6][2][3][5][4] as a collection of 6 "shelves", each having 2 books, each having 3 pages, each having 5 rows, each having 4 columns.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.