The same way a 1D array is an array of single (0D) values, the simplest way to think about a 2D array is as an array of equal- and fixed-sized 1D arrays, and a 3D array as an array of 2D arrays. More generally, an ND array can be thought of as an array of [N-1]D arrays.
[N.B. skim this section to get an idea of the meanings of the terms, then look at the examples below, then come back to these concepts]
Multi-dimensional Data Structures are modelsAlmost all computer programs implement a model of a process in the real world. The data structures of that program store the data of the model. If the real world process has data that are multi-dimensional, then it may make sense to store model data in a multi-dimensional data structure. That is why multi-dimensional data structures have been implemented in most programming languages.
Model Data
Each datum in a multi-dimensional data structure typically has the same data type (e.g. integer, real, string, UDT) as all other data in that structure, and each datum represents the same physical quantity of the model. For example, a single datum could represent a numerical scalar, such as a temperature, or a brightness, or a time, or a product presence/absence, or a product size, or a product type, or a product quanity; a single datum could also represent some combination of such scalar values combined into a UDT.
Model Dimensions
Each of the model's dimensions in a 2D or 3D data structure is (typically) independent of (i.e. orthogonal to) the other dimensions; depending on the model, that orthogonality may be spatial (right-left vs. up-down; East-West vs. North-South), or that orthogonality may conceptual (spatial vs. time).
Model Data Location - Index Tuples
Each datum stored in a multi-dimensional data structure has a unique memory location in that structure, and each unique memory location is identified by a unique pair (2D) or triplet (3D) or tuple (N-D) of indices (index values), one index for each dimension. An index value represents a real-world parameter structure other than the datum of the data model itself. Most 2D and 3D data are spatial in nature E.g. The 2D data example below has a West-to-East position offset as the first dimension (index), and a South-to-North position offset as the second dimension, all relative to the starting corner at southwest extent of the area being modeled. However, any index could represent something other than a spatial dimenension if required by the model: time; color; etc.
Example 1: digital imagesIn that 2D image,
- the data are pixels (picture elements),
- each pixel's value represents the color of that pixel
- each pixel's location is uniquely identified by the pair of indices:
- its spatial offset from the left edge (1st dimension)
- its spatial offset from the bottom edge (2nd dimension)
- For example, the indicated pixel
- is at offset i1 along the 1st dimension from the left edge, and
- is at offset i2 along the second from the bottom edge), and
- its datum value could be the color name string "dark green."
- Or, if using the RGB (Red-Green-Blue) color model, the datum value could be the triplet (Red=0%, Green=50%, 0%), or (0,50,0)
- In that case, the image could be considered a 3D data structure with three dimensions:
- spatial (West-to-East);
- spatial (South-to-North);
- base color (Red, Green, Blue).
Example 2: Time SamplingConsider a process that produces a tangible product, such as a bottle of water. The PLC controlling the process counts the numbers of bottles produced over each hour, 24 hours per day from midnight to midnight. It also keeps track of those hourly counts from yesterday, the day before yesterday, going back one week (7 days). To model those data, the progam would create a 2D array, COUNTS, with 24 elements (index offsets [0..23]) in the first dimension to model the hours in each day, and with 7 elements (index offsets [0..7]) in the second dimension to model the current day plus the past 7 days.
So if day-index of 0 represents today, the datum value at COUNTS array location COUNTS[13,2] would be the count of bottles produced between 1PM and 2PM on the day before yesterday.
At the start of each day i.e. immediately after midnight, the 24 data in 1D array COUNTS[0..23,0] just completed becomes "yesterday's" data, and the 24 data in 1D array COUNTS[0..23,7] are obsolete. To model this transition, the 24 data in COUNTS[0..23,6] are COPied to COUNTS[0..23,7] (Note 1), the 24 data in COUNTS[0..23,5] are COPied to COUNTS[0..23,6], etc., the 24 data in COUNTS[0..23,0] are COPied to COUNTS[0..23,1] (yesterday), and finally the 24 data in COUNTS[0..23] are zeroed out with a FLL instruction to initialize this new days counts before today's first bottle is produced.
Note 1: in actuality, all of those data in that array are in a contiguous 192 (=24 x 8) memory locations, with the first element COUNTS[0,day] of one day immediately after the last element COUNTS[23,day-1], so the entire 24-position shift of 7 days of data (168 elements) could be done with but two COP instructions and a temporary intermediate buffer [COP COUNTS[0,0] buffer168[0] COP buffer169[0] COUNTS[0,1] FLL 0 COUNTS[0,0] 24]
Memory Layout of Multi-Dimensional Data ArraysComputer memory is always linear i.e. 1-dimensional, and as mentioned in Note 1 above a multi-dimensional array is laid out as a 1D stream of contiguous memory locations. It is only the notation, e.g. COUNTS[i1,i2] that emulates an actual 2D data structure. Specifically, the notation COUNTS[i1,i2] refers to a memory location that is offset (i1 + (24*i2)) elements from the memory location of the first element, COUNTS[0,0] in COUNTS. So there is nothing special about multi-dimensional arrays, and they could be as easily represented by a long single 1D array, e.g. COUNTS_1D[0..191], with the program calculating the 1D offset e.g. COUNTS_1D[i1 + (24*i2)], instead of declaring a 2D array COUNTS[0..23],0..7] and using the syntax COUNTS[i1,i2].