## 传递数组示例

### `print2darray` 函数

`shrlibsample` 库中的 `print2darray` 函数显示列数为三、行数可变的二维数组的值。参数 `my2d``double` 类型的二维数组。参数 `len` 是行数。

```EXPORTED_FUNCTION void print2darray(double my2d[][3],int len) { int indxi,indxj; for(indxi=0;indxi<len;++indxi) { for(indxj=0;indxj<3;++indxj) { mexPrintf("%10g",my2d[indxi][indxj]); } mexPrintf("\n"); } }```

### 将 MATLAB 数组转换为 C 样式的维度

```if not(libisloaded('shrlibsample')) addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample') end```

`m = reshape(1:12,4,3)`
```m = 4×3 1 5 9 2 6 10 3 7 11 4 8 12 ```

`calllib('shrlibsample','print2darray',m,4)`
``` 1 2 3 4 5 6 7 8 9 10 11 12 ```

`calllib('shrlibsample','print2darray',m',4)`
``` 1 5 9 2 6 10 3 7 11 4 8 12 ```

### `multDoubleArray` 函数

`shrlibsample` 库中的 `multDoubleArray` 函数将数组中的每个元素乘以三。该函数使用单一下标（线性索引）来导航输入数组。

```EXPORTED_FUNCTION void multDoubleArray(double *x,int size) { /* Multiple each element of the array by 3 */ int i; for (i=0;i<size;i++) *x++ *= 3; }```

MATLAB® 函数签名是：

`doublePtr``multDoubleArray````(doublePtr, int32)```

### 保留三维 MATLAB 数组

```if not(libisloaded('shrlibsample')) addpath(fullfile(matlabroot,'extern','examples','shrlib')) loadlibrary('shrlibsample') end```

```vin = reshape(1:20,2,5,2); vs = size(vin)```
```vs = 1×3 2 5 2 ```

```vout = calllib('shrlibsample','multDoubleArray',vin,20); size(vout)```
```ans = 1×2 2 10 ```

```vout = reshape(vout,vs); size(vout)```
```ans = 1×3 2 5 2 ```