📚 2 Arrays: Two-Dimensional Data Structures for Scientific Computing | 二维数组:科学计算中的二维数据结构
When scientists collect repeated measurements in structured grids — like daily temperatures across a map or light intensity on a sensor grid — they need an organised way to store and process the data. A two-dimensional array (2D array) is the fundamental programming structure that represents such tabular or matrix-shaped information. It is simply an array of arrays, with rows and columns that can be indexed with two coordinates, much like cells in a spreadsheet or positions on a chessboard.
当科学家在结构化网格中收集重复测量数据时——例如地图上的每日温度或传感器网格上的光强度——他们需要一种井然有序的方式来存储和处理数据。二维数组(2D array)正是表示这类表格或矩阵状信息的基本编程结构。它本质上就是数组的数组,拥有可通过两个坐标索引的行和列,就像电子表格中的单元格或棋盘上的位置一样。
1. What Is a 2D Array? | 什么是二维数组?
A 2D array can be visualised as a rectangle of elements organised in rows and columns. Each element is identified by two indices: the first index selects the row, and the second selects the column. In many programming languages the first row is index 0 and the first column is also index 0, so the top‑left element is at position [0][0]. Mathematically this resembles a matrix with m rows and n columns.
二维数组可以想象成一个由行和列组成的矩形元素集合。每个元素由两个索引来标识:第一个索引选择行,第二个索引选择列。在许多编程语言中,第一行的索引为 0,第一列索引也为 0,因此左上角元素位于 [0][0] 位置。从数学角度看,这类似于一个具有 m 行 n 列的矩阵。
Array_name[row_index][column_index]
数组名称[行索引][列索引]
2. Why Scientists Need 2D Arrays | 科学家为什么需要二维数组
In physics, a time‑series of temperature readings at several spatial locations forms a 2D dataset (time vs. position). In chemistry, spectral data can be recorded as intensity against wavelength for multiple samples. In biology, the growth of bacterial colonies on a grid of agar plates naturally maps to a 2D array. Any scientific investigation that records a dependent variable over two independent variables will fit into this model.
在物理中,多个空间位置上的温度读数随时间变化构成一个二维数据集(时间 vs. 位置)。在化学中,光谱数据可以记录为多个样本的强度随波长的变化。在生物学中,琼脂平板网格上的细菌菌落生长自然会映射到一个二维数组。任何在两种自变量上记录因变量的科学探究都可以套用这个模型。
3. Declaring and Initialising a 2D Array | 声明并初始化二维数组
st specify the data type and the number of rows and columns. In some languages it is a two‑ sts. For example, a 3×3 identity matrix could be initialised as [[1,0,0],[0,1,0],[0,0,1]].
要创建二维数组,必须指定数据类型以及行数和列数。在某些语言中这是一个两步过程:首先创建一个行数组,然后将每一行变成一个包含所需列数的数组。数组可以用默认值填充,可以从文件中加载,也可以直接用列表的列表赋值。例如,一个 3×3 的单位矩阵可以初始化为 [[1,0,0],[0,1,0],[0,0,1]]。
- data_type array_name[rows][columns]; — typical declaration in languages like C or C++.
- 数据类� array_name[行数][列数]; — 类似 C 或 C++ 中的典型声明。
4. Accessing and Modifying Elements | 访问与修改元素
Each cell in a 2D array is reached by its row and column index. You can read the value stored there or assign a new value. Care must be taken not to exceed the array bounds, as attempting to access an index of -1 or an index equal to the size will cause an error. In scientific data processing it is common to update a whole row at a time or to apply a formula to every cell.
二维数组中的每个单元格都通过它的行索引和列索引来访问。你可以读取存储在那里的值,也可以赋予新值。必须注意不要越界,因为试图访问 -1 或者等于尺寸的索引会导致错误。在科学数据处理中,一次更新一整行或者对每个单元格应用公式是很常见的操作。
value = grid[row][col]; grid[row][col] = new_value;
值 = 网格[行][列]; 网格[行][列] = 新值;
5. Traversing a 2D Array with Nested Loops | 用嵌套循环遍历二维数组
The most common pattern for visiting every element is to use an outer loop that iterates over the rows and an inner loop that scans the columns. This row‑major order matches the way most languages store 2D arrays in memory. Traversal is essential for finding minimum or maximum values, computing averages, or filtering data based on a condition.
访问每一个元素最常用的模式是使用外层循环遍历行、内层循环扫描列。这种行优先顺序与大多数语言在内存中存储二维数组的方式相匹配。遍历对于查找最小值或最大值、计算平均值或根据条件筛选数据至关重要。
for row = 0 to rows-1
for col = 0 to cols-1
process grid[row][col]
6. Scientific Example: Temperature Grid | 科学示例:温度网格
4 grid. The data can be stored in a 2D array temperature[4][4]. To find the average temperature of the whole field you sum all elements and divide by 16. To locate the hottest point you can track the maximum during traversal. This is exactly how satellite land‑surface temperature maps are processed, just on a much larger scale.
想象一个气象站,每小时记录布置在 4×4 网格上的十六个地点的地面温度。数据可以存储在二维数组 temperature[4][4] 中。要计算整个区域的平均温度,就把所有元素加起来然后除以 16。要找到最热点,可以在遍历过程中追踪最大值。这正是卫星地表温度图处理的方式,只不过规模要大得多。
7. Scientific Example: Light Intensity on a Sensor | 科学示例:传感器上的光强度
Digital cameras use a 2D array of millions of tiny light sensors. Each sensor produces a number that represents the brightness of the light falling on it. For a monochrome image, the 2D array of pixel values directly mirrors the scene. Scientists use such arrays to analyse diffraction patterns, observe the motion of particles, or measure the absorbance of a solution in a microplate reader where each well corresponds to a row and column.
数码相机使用由数百万个微型光传感器构成的二维阵列。每个传感器都产生一个数字,表示照射到它上面的光强度。对于单色图像来说,像素值的二维数组直接反映了所拍摄的场景。科学家利用此类数组来分析衍射图样、观察粒子运动,或是测量酶标仪中溶液的吸光度,其中每个孔对应一个行和列。
8. 2D Arrays Versus Spreadsheets | 二维数组与电子表格
A spreadsheet is essentially a user‑friendly 2D array. Cells are labelled by column letter and row number (e.g. C5), much like [2][4] in a zero‑indexed array. Many scientific experiments start with data entered in a spreadsheet, which is then exported and loaded into a programming environment as a 2D array for automated analysis, curve fitting, and statistical tests.
电子表格本质上就是一个用户友好的二维数组。单元格用列字母和行号来标记(例如 C5),这与从零索引的数组中的 [2][4] 非常相似。许多科学实验的第一步都是在电子表格中录入数据,随后导出并作为二维数组加载到编程环境中,用于自动分析、曲线拟合和统计检验。
| Spreadsheet | 2D Array |
|---|---|
| A1 | [0][0] |
| B3 | [2][1] |
| C5 | [4][2] |
9. Memory Layout and Efficiency | 内存布局与效率
In row‑major order, elements of a row are stored consecutively in memory. This means that accessing an entire row sequentially is fast, while jumping between columns may be slower due to cache behaviour. When processing large scientific datasets, it is wise to design loops so that the innermost loop moves along columns, thereby re‑using the same row as much as possible before moving to the next row.
在行优先布局中,同一行的元素在内存中是连续存储的。这意味着按顺序访问一整行很快,而频繁在不同列之间跳转可能会因为缓存行为而变慢。在处理大型科学数据集时,明智的做法是设计循环,让最内层循环沿列移动,从而尽可能多地重用同一行,然后再转到下一行。
10. Common Pitfalls and Debugging Tips | 常见陷阱与调试技巧
Off‑by‑one errors occur when loop limits are set incorrectly, especially if you confuse the number of rows with the maximum index. Another frequent mistake is forgetting to initialise inner arrays, leading to null references. Printing a small 2D array with row and column headers helps to visualise the data. In scientific work, always validate your array by checking its dimensions and a few known sample points after reading from a file.
当循环边界设置错误时会发生差一错误,尤其是混淆行数与最大索引值的时候。另一个常见错误是忘记初始化内部数组,导致空引用。打印一个带行头和列头的小型二维数组有助于可视化数据。在科学工作中,从文件读取数据后,务必通过检查数组的维数和几个已知样本点来验证数组。
- Always use rows = array.length and cols = array[0].length.
- 始终使用 行数 = 数组.length 和 列数 = 数组[0].length。
- Test boundary conditions: first row, last row, first column, last column.
- 测试边界条件:第一行、最后一行、第一列、最后一列。
11. Beyond Two Dimensions | 超越二维
If the collected data depends on three independent variables — for instance, air pressure measured at different latitudes, longitudes and altitudes — a 3D array becomes necessary. Scientific modelling of a volume, such as a CT scan or a climate simulation, routinely uses three‑dimensional arrays. The same principles extend: indexing with three coordinates and traversal with triply nested loops. Higher‑dimensional arrays are also possible but can become difficult to visualise.
如果采集的数据依赖于三个自变量——例如在不同纬度、经度和高度上测量的大气压——就需要三维数组。对三维体积的科学建模,如 CT 扫描或气候模拟,通常会使用三维数组。同样的原则可以扩展:用三个坐标进行索引,用三重嵌套循环进行遍历。更高维度的数组也是可能的,但会变得难以直观想象。
12. Summary | 总结
Two‑dimensional arrays are the backbone of structured scientific data processing, enabling scientists to store, access and manipulate grid‑based measurements with clarity and efficiency. Whether you are analysing temperature fields, microscope images, or spectrometry results, mastering rows, columns, indices and nested loops is an essential skill. The concepts transfer directly to spreadsheet logic and higher‑dimensional datasets, forming a foundation for computational experiments across all sciences.
二维数组是结构化科学数据处理的脊梁,使科学家能够清晰高效地存储、访问和操作基于网格的测量数据。不论你是在分析温度场、显微镜图像还是光谱测定结果,掌握行、列、索引和嵌套循环都是至关重要的技能。这些概念可以直接迁移到电子表格逻辑和更高维度的数据集,为所有科学领域的计算实验奠定基础。
Published by TutorHao | Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图