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.

I know that when we speak about an array having 1, 2, or 4 dimensions, we mean arrays like this:

1: [0]
2: [0,0]
3: [0,0,0]
4: [0,0,0,0]
...

Is the first 'axis' of an array the only thing that defines its dimensionality? Can an array with 4 dimensions be represented these ways?

[0, [0,0,0]]
[[0,0], [0,0]]

or does this fall under some other definition of dimensionality?

Another option is that a 4-dimensional array would be of the form

[ [[ [0,..],[0,..] ], [ [0,..],[0,..] ]], [[ [0,..],[0,..] ], [ [0,..],[0,..] ]], [[ [0,..],[0,..] ], [ [0,..],[0,..] ]], [[ [0,..],[0,..] ], [ [0,..],[0,..] ]] ]

e.g.

[ 
  [
    [ [0,..],[0,..] ], 
    [ [0,..],[0,..] ]
  ], 
  [
    [ [0,..],[0,..] ], 
    [ [0,..],[0,..] ]
  ], 
  [
    [ [0,..],[0,..] ], 
    [ [0,..],[0,..] ]
  ], 
  [
    [ [0,..],[0,..] ], 
    [ [0,..],[0,..] ]
  ]
]

Is this just a nomenclature nuance that I'm bringing up? Is this definition relative to the field in which we are discussing (computation vs. physical systems)?


(the thoughts came from this question.)

share|improve this question
1  
I think your second examples (such as [0, [0,0,0]] and [[0,0], [0,0]]) look like jagged arrays - basically, an array of arrays of any size (msdn.microsoft.com/en-us/library/2s05feca.aspx) –  FrustratedWithFormsDesigner Jul 3 at 19:28
    
If you have access to MATLAB or Octave, the concept of "singleton dimension" would be tremendously useful to help anyone think through the issue. See: mathworks.com/help/matlab/ref/squeeze.html –  rwong Jul 7 at 21:44
    
Could you please specify the notation you're using? It looks to me like your first example is a normal 1D array but based on the text, it isn't. –  Trenton Maki Jul 7 at 21:53

5 Answers 5

1: [0]
2: [0,0]
3: [0,0,0]
4: [0,0,0,0]
...

I think you've got it wrong. If you mean that these are one-/two-/three-/four-dimensional arrays, that's not the case: they're all one-dimensional, they just have different lengths.

But length is not dimensionality. As whatsisname explained, the dimensionality of an entire array is the number of subscripts you need to give in order to address a single element. The length is just the number of different values you can legally give for one particular subscript in that expression (if the length is 4, the valid values are 0,1,2,3).

share|improve this answer
    
Ok, but I give examples of nested arrays, too — or so I thought. This nesting could be described by subscripts? Is this just about notation, where I used 'wonky' notation since I was trying to ask if [tree-like] nesting was the same thing as dimensionality? –  New Alexandria Jul 4 at 18:00

Dimensionality comes from ... dimensions!

A one-dimensional array is like one of those daily pill containers:

pill container

It's a vector, with a single index, and you can select one specific element at a time by specifying where along the line it lies.

A two-dimensional array is like a chessboard:

enter image description here

It's a matrix, with two indices, and you can select one specific element at a time by specifying where the two lines intersect. In chess notation, "a1" is the bottom left cell in the image, "h8" the top right, in most languages that would be [0,0] and [7,7] or [0][0] and [7][7].

A three-dimensional array is like a Rubik's cube (except that you can access more than just the surface elements):

enter image description here

It's a cube, with three indices, and you can select one specific element at a time by specifying where the three lines intersect.

A four-dimensional array is like ... well ... nothing real. But mathematicians have been modeling higher-dimensional structures for a long time, and some even have names. You'll have to just extend your mind to grasp them. But the computer equivalent is an even-more-natural extension,

share|improve this answer
    
A 4 dimensional array could be thought of as a row of cubes, and so on. –  Kyranstar Jul 8 at 19:26

The dimensionality is the number of subscripts you can use to select elements.

Your example [0, [0,0,0]] is still a 2d array, albeit containing a 3d array as its second element. That doesn't make it a 4d or 5d array, just a nested data structure where the "dimensionality" concept breaks down and gets confusing real fast.

Examples like [[0,0], [0,0]] and [0, [0,0,0]] are thought of better not as arrays but as trees or other hierarchical data structures.

share|improve this answer

Yes the examples given are all 1 dimensional as Kilan states, though it could also depend on exactly what you are using, e.g. computer languages which have different format to represent such constructs.

Dimensionality itself can also be thought of as 'nesting', e.g. if there is one set of discrete elements that is 1 dimension, if each elements also has elements, that's 2 dimensional, if each of those also is compose of elements, 3 dimensionsal, etc.

In ruby (for example) this is represented by:

[n] # One Dimension
[n][o] # Two Dimensions
[n][o][p] # Three Dimensions
[n][o][p][q] # Four Dimensions

For each case n, o, p and q are the number of elements at that level.

So, for example if you wanted to represent 6 US states, the top 4 cities in each and the tallest 5 buildings in each city you could use [5][3][4] (assuming the use of zero based arrays).

I have also worked with languages though where a 3 dimensional array such as the above would be represented by

(5,3,4)
share|improve this answer
    
I cannot see why the nested(?) arrays that I listed in my question are different than the nexting that you've answered. The only difference, to me, looks like notation. Yes? –  New Alexandria Jul 4 at 17:57

An n-dimensional array is a collection of data. This data can be accessed by a value.

People often associate this value with coordinate in an n-dimensional space so this value would have n components say (x,y,z) for 3-d array. In other words, the number of dimensions in an array is the number of components in the value you use to access data in the array.

Now comes the thing that few people already mentioned in their answer which is they say array is not the same as tree hierarchy. This is because we can all sense that there is something difference between a tree and a cube other than notation but many can't say what the difference is. After all, data in a tree and data in an n-dimension array can be identified by a vector like value.

I am surprised that nobody has spotted the difference yet. It is simple and trivial. The difference is in the way they grow. Tree grows in depth, which means their branching factor stays the same. On the other hand, cube (array) grows in length which means their depth stays the same (same number of dimension) but the branching factor changes if you see them from tree like perspective. This results in one having exp grow and minimum compression, while there is the opposite for the other. From a computer software viewpoint, you can see the difference in the way you declare a variable.

share|improve this answer

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.