Main Content

## 逗号分隔的列表

### 什么是逗号分隔的列表？

```1,2,3 ```
```ans = 1 ans = 2 ans = 3 ```

### 生成逗号分隔的列表

#### 从元胞数组生成列表

```C = cell(4,6); for k = 1:24 C{k} = k*2; end C ```
```C = 4×6 cell array {[2]} {[10]} {[18]} {[26]} {[34]} {[42]} {[4]} {[12]} {[20]} {[28]} {[36]} {[44]} {[6]} {[14]} {[22]} {[30]} {[38]} {[46]} {[8]} {[16]} {[24]} {[32]} {[40]} {[48]}```

```C{:,5} ```
```ans = 34 ans = 36 ans = 38 ans = 40 ```

`C{1,5},C{2,5},C{3,5},C{4,5}`

#### 从结构体生成列表

```S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2); S.f5 ```
```ans = 34 ans = 36 ans = 38 ans = 40 ```

`S(1).f5,S(2).f5,S(3).f5,S(4).f5`

### 从逗号分隔的列表分配输出

```C = cell(4,6); for k = 1:24 C{k} = k*2; end [c1,c2,c3,c4,c5,c6] = C{1,1:6}; c5 ```
```c5 = 34 ```

`[c1,c2,c3] = C{1,1:6};`

```S = cell2struct(C,{'f1','f2','f3','f4','f5','f6'},2); [sf1,sf2,sf3] = S.f5; sf3 ```
```sf3 = 38 ```

### 为逗号分隔的列表赋值

```c{1} = []; c{2} = []; c{:} ```
```ans = [] ans = []```

```[c{:}] = deal([10 20],[14 12]); c{:} ```
```ans = 10 20 ans = 14 12```

```s(1).field1 = [[]]; s(2).field1 = [[]]; s.field1 ```
```ans = [] ans = []```

```[s.field1] = deal([10 20],[14 12]); s.field1 ```
```ans = 10 20 ans = 14 12 ```

### 如何使用以逗号分隔的列表

#### 构造数组

```C = cell(4,6); for k = 1:24 C{k} = k*2; end A = {'Hello',C{:,5},magic(4)} ```
```A = 1×6 cell array {'Hello'} {[34]} {[36]} {[38]} {[40]} {4×4 double}```

```A = {'Hello',C,magic(4)} ```
```A = 1×3 cell array {'Hello'} {4×6 cell} {4×4 double}```

#### 显示数组

```A{:} ```
```ans = 'Hello' ans = 4×6 cell array {[2]} {[10]} {[18]} {[26]} {[34]} {[42]} {[4]} {[12]} {[20]} {[28]} {[36]} {[44]} {[6]} {[14]} {[22]} {[30]} {[38]} {[46]} {[8]} {[16]} {[24]} {[32]} {[40]} {[48]} ans = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1```

#### 串联

```A = [C{:,5:6}] ```
```A = 34 36 38 40 42 44 46 48 ```

#### 用作函数调用参量

```X = -pi:pi/10:pi; Y = tan(sin(X)) - sin(tan(X)); C = cell(2,3); C{1,1} = 'LineWidth'; C{2,1} = 2; C{1,2} = 'MarkerEdgeColor'; C{2,2} = 'k'; C{1,3} = 'MarkerFaceColor'; C{2,3} = 'g'; figure plot(X,Y,'--rs',C{:}) ```

#### 用作函数返回值

MATLAB 函数也可以向调用方返回多个值。这些值以列表形式返回，并且每个值用逗号分隔。您可以将逗号分隔的列表与结构体或元胞数组一起使用，而不用列出每个返回值。对于返回值的数量可变的函数，这一用法更为实用。

```C = cell(1,3); [C{:}] = fileparts('work/mytests/strArrays.mat') ```
```C = 1×3 cell array {'work/mytests'} {'strArrays'} {'.mat'}```

### 快速傅里叶变换示例

`fftshift` 函数交换数组的每个维度的左半部分和右半部分。对于向量 `[0 2 4 6 8 10]`，输出为 `[6 8 10 0 2 4]`。对于多维数组，`fftshift` 沿每个维度执行此交换。

`fftshift` 使用索引向量来执行交换。对于上面显示的向量，索引 `[1 2 3 4 5 6]` 经过重新排列以形成新索引 `[4 5 6 1 2 3]`。然后，该函数使用此索引向量重新定位各元素。对于多维数组，`fftshift` 为每个维度构造一个索引向量。逗号分隔的列表使该任务变得很简单。

```function y = fftshift(x) numDims = ndims(x); idx = cell(1,numDims); for k = 1:numDims m = size(x,k); p = ceil(m/2); idx{k} = [p+1:m 1:p]; end y = x(idx{:}); end ```

``` if ndims(x) == 1 y = x(index1); else if ndims(x) == 2 y = x(index1,index2); end end ```

### 使用以逗号分隔的列表对运算进行故障排除

#### 中间索引生成了以逗号分隔的列表

`A = {magic(3),rand(3)}`
```A = 1×2 cell array {3×3 double} {3×3 double}```

`A{1,:}`
```ans = 8 1 6 3 5 7 4 9 2 ans = 0.7922 0.0357 0.6787 0.9595 0.8491 0.7577 0.6557 0.9340 0.7431```

`A{1,:}(1,2)`
```Intermediate brace '{}' indexing produced a comma-separated list with 2 values, but it must produce a single value when followed by subsequent indexing operations.```

```A{1,1}(1,2) A{1,2}(1,2)```
```ans = 1 ans = 0.0357```

#### 表达式生成了以逗号分隔的列表，而不是单个值

```A = cell(1,3); A{1} = 2; A{2} = 3; A{3} = 5;```

`A{:}` 生成一个包含三个值的以逗号分隔的列表。

`A{:}`
```ans = 2 ans = 3 ans = 5```

```for c = A{:} disp(c) end```
```A brace '{}' indexing expression produced a comma-separated list with 3 values where only a single value is allowed.```

```for c = [A{:}] disp(c) end```
``` 2 3 5```

#### 使用简单赋值对多个元素赋值

`B = cell(2,3);`

`B{:} = 5`
```Assigning to 6 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.```

`[B{:}] = deal(5)`
```B = 2×3 cell array {[5]} {[5]} {[5]} {[5]} {[5]} {[5]}```

#### 使用以逗号分隔的列表对未定义变量赋值

```x{1} = []; x{2} = []; [x{:}] = deal([10 20],[14 12]); x{:} ```
```ans = 10 20 ans = 14 12```

```[y{:}] = deal([10 20],[14 12]); ```
```Comma-separated list assignment to a nonexistent variable is not supported when any index is a colon (:). Use explicit indices or define the variable before performing the assignment.```

`[y{1:2}] = deal([10 20],[14 12])`
```y = 1×2 cell array {[10 20]} {[14 12]}```