matlab study notes 12_4rmfield, arrayfun, structfun, struct2cell, cell2struct

Learn together matlab-matlab study notes 12

12_4 structure

rmfield,arrayfun,structfun,struct2cell,cell2struct

Find useful, welcome to discuss mutual learning together - Follow Me

Reference
https://ww2.mathworks.cn/help/matlab/ref/rmfield.html
https://ww2.mathworks.cn/help/matlab/ref/arrayfun.html
https://ww2.mathworks.cn/ Help / MATLAB / ref / structfun.html
https://ww2.mathworks.cn/help/matlab/ref/struct2cell.html
https://ww2.mathworks.cn/help/matlab/ref/cell2struct.html

refield

  • Delete the structure of the field
  • s = rmfield(s,field)
  • s = rmfield (s, field) to delete the specified one or more fields from a structural array in s. Character vector using cellular array or a string array specifies a plurality of fields. s dimensions remain unchanged.
定义一个包含 first、second、third 和 fourth 字段的标量结构体。

S.first = 1;
S.second = 2;
S.third = 3;
S.fourth = 4;
删除字段 first 和 fourth。

fields = {'first','fourth'};
S = rmfield(S,fields)
S = struct with fields:
    second: 2
     third: 3

arrayfun

  • The function is applied to each array element, except that the input parameters must be scalar structfun structure.

    grammar

  • B = arrayfun(func,A)
    • B = arrayfun (func, A) is applied to the A function func elements, one at a time element. The output func arrayfun then concatenated into the output array B, the, for the i th element of A is, B (i) = func ( A (i)). Func function handle input parameter is a function, this function takes an input parameter and returns a scalar. func The output data may be any type, as long as the objects of this type can be connected in series. Arrays A and B must have the same size.
      You can not specify the order arrayfun calculated for each element B, we can not expect them to complete the calculation in any particular order.
创建一个非标量结构体数组。每个结构体有一个包含随机数向量的字段。这些向量具有不同的大小。

S(1).f1 = rand(1,5);
S(2).f1 = rand(1,10);
S(3).f1 = rand(1,15)
S = 1x3 struct array with fields:
    f1

使用 arrayfun 函数计算 S 中每个字段的均值。不能使用 structfun 完成此计算,因为 structfun 的输入参数必须是标量结构体。

A = arrayfun(@(x) mean(x.f1),S)
A = 1×3

    0.6786    0.6216    0.6069
  • B = arrayfun(func,A1,...,An)
    • B = arrayfun (func, A1, ..., An) is applied to the array of func A1, ..., An of the element, B (i) = func (A1 (i), ..., An (i) ). Function func must receive the n input parameters and returns a scalar. Array A1, ..., An must all be the same size.
创建一个结构体数组,其中每个结构体有两个包含数值数组的字段。

S(1).X = 5:5:100; S(1).Y = rand(1,20);
S(2).X = 10:10:100; S(2).Y = rand(1,10);
S(3).X = 20:20:100; S(3).Y = rand(1,5)
S = 1x3 struct array with fields:
    X
    Y

绘制数值数组。从 plot 函数返回一个图形线条对象的数组,并使用这些对象为每一组数据点添加不同的标记。arrayfun 可以返回任何数据类型的数组,只要该数据类型的对象可以串联即可。

figure
hold on
p = arrayfun(@(a) plot(a.X,a.Y),S);
p(1).Marker = 'o';
p(2).Marker = '+';
p(3).Marker = 's';
hold off

Here Insert Picture Description

  • B = arrayfun( ___ ,Name,Value)
    • B = arrayfun (___, Name, Value) func application and one or more Name, Value parameter specifies the group of other options. For example, to return the output value to form a cellular array, specify 'UniformOutput', false. When the value is not returned func series into arrays and to be returned in the form of B. Cellular array You may be Name, Value of the input parameter set of parameters used in conjunction with any of the above syntax.
      `` `
      Create a non-scalar structure array. Each field has a structure comprising a matrix of values.

S(1).f1 = rand(3,5);
S(2).f1 = rand(6,10);
S(3).f1 = rand(4,2)
S = 1x3 struct array with fields:
f1

S is calculated using the Mean arrayfun function of each field. Returns mean vector contains the mean of each column, and therefore can not return an array of the mean. To return to the form of the mean cellular array, specify 'UniformOutput', false name - value pairs.

A = arrayfun(@(x) mean(x.f1),S,'UniformOutput',false)
A = 1x3 cell array
{1x5 double} {1x10 double} {1x2 double}
```

  • [B1,...,Bm] = arrayfun( ___ )
    • When the m output values ​​returned func, [B1, ..., Bm] = arrayfun (___) returns an array of a plurality of outputs B1, ..., Bm. func may return output parameters of different data types, the data types of each output is returned each time a call must be the same func. This may be combined with any of the foregoing input parameters grammar syntax used.
    • The number of output parameters returned from the func may differ from A1, ..., An amount of the specified input parameters.
创建一个非标量结构体数组。

S(1).f1 = 1:10;
S(2).f1 = [2; 4; 6];
S(3).f1 = []
S = 1x3 struct array with fields:
    f1

使用 arrayfun 函数计算 S 中每个字段的大小。行数和列数分别输出在两个 1×3 数值数组中。

[nrows,ncols] = arrayfun(@(x) size(x.f1),S)
nrows = 1×3

     1     3     0

ncols = 1×3

    10     1     0

structfun

  • Each field of an application for a scalar function of the structure - and arrayfun different, all elements arrayfun applied function, structfun all fields applied function

  • A = structfun(func,S)
    • A = structfun (func, S) is applied to each field to the function func scalar structure of S, each time a field. The output func structfun then concatenated into a column vector A. Func function handle input parameter is a function, this function takes an input parameter and returns a scalar. func The output data may be any type, as long as the objects of this type can be connected in series. A number of elements equal to the number S of the field.
      `` `
      Create a scalar structure, which field contains an array of values of different sizes.

S.f1 = 1:10;
S.f2 = [2; 4; 6];
S.f3 = []
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x1 double]
f3: []

Calculate the mean value for each array, and returns an array of the mean.

A = structfun(@mean,S)
A = 3×1

5.5000
4.0000
NaN

* A = structfun(func,S,Name,Value)
  * A = structfun(func,S,Name,Value) 应用 func 并使用一个或多个 Name,Value 对组参数指定其他选项。例如,要以结构体形式返回输出值,**请指定 'UniformOutput',false。** 当 func 返回的值不能合并为数组时,可以按结构体形式返回 A。返回的结构体具有与 S 相同的字段。

Create a scalar structure, which comprises a matrix field.

S.f1 = 1:10;
S.f2 = [2 3; 4 5; 6 7];
S.f3 = rand(4,4)
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x2 double]
f3: [4x4 double]

Calculate the mean of each matrix. Returns mean vector contains the mean of each column, and therefore can not return an array of the mean. To return to the mean structural form, specify 'UniformOutput', false name - value pairs.

A = structfun(@mean,S,'UniformOutput',false)
A = struct with fields:
f1: 5.5000
f2: [4 5]
f3: [0.6902 0.3888 0.7627 0.5962]


* [A1,...,Am] = structfun( ___ )
  * 当 func 返回 m 个输出值时,[A1,...,Am] = structfun(_ __ ) 返回多个输出数组 A1,...,Am。func 可以返回不同数据类型的输出参数,但每次调用 func 时返回的每个输出的数据类型必须相同。可以将此语法与前面语法中的任何输入参数结合使用。

Create a scalar structure.

S.f1 = 1:10;
S.f2 = [2 3; 4 5; 6 7];
S.f3 = rand(4,4)
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x2 double]
f3: [4x4 double]

It calculates the size S of each array. Are the number of rows and columns of a 3 × 1 array of numbers.

[nrows,ncols] = structfun(@size,S)
nrows = 3×1

 1
 3
 4

ncols = 3×1

10
 2
 4

```


struct2cell

  • Converting the cellular array structure is a
  • C = struct2cell(S)
  • C = struct2cell (S) to convert the cellular array structure. Cellular arrays containing C S values ​​copied from a field.
  • struct2cell function does not return the field name. To return to the field names in the cellular array, use fieldnames function.
创建一个结构体。

S.x = linspace(0,2*pi);
S.y = sin(S.x);
S.title = 'y = sin(x)'
S = struct with fields:
        x: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(x)'

将 S 转换为元胞数组。

C = struct2cell(S)
C = 3x1 cell array
    {1x100 double}
    {1x100 double}
    {'y = sin(x)'}

元胞数组不包含字段名称。要返回元胞数组中的字段名称,请使用 fieldnames 函数。fieldnames 和 struct2cell 以相同的顺序返回字段名称和值。

fields = fieldnames(S)
fields = 3x1 cell array
    {'x'    }
    {'y'    }
    {'title'}

cell2struct

  • Converting the cellular array is an array of structures
  • structArray = cell2struct(cellArray, fields, dim)
  • structArray = cell2struct (cellArray, fields, dim) to create an array of structures structArray cellular array CellArray through information contained.
  • Field Name fields specified structure body array parameters. This parameter is a character array, the character string or the vector cellular array of arrays.
  • MATLAB® dim parameter indicates the axis to create an array of cellular array structure is to be used. Using the specified values ​​double dim.
  • To obtain a field from the N rows in cellular array to create an array of structures, N fields specify the name parameter in the fields specified in a digital dim parameter. To obtain a field from an array of M columns cellular create an array of structures, specify the M parameter field name fields, number 2 in dim specify parameters.
  • structArray output member is an array of structures having N fields, where N equals the number of fields in the input fields parameter. The number of fields of the structure must be generated in a number equal to convert Cellular dimensions of dim.

Examples

  • In this section an example of a table created under. The table lists information about the employees of a small engineering company. Read rows in the table will display the name of the employee by sector. By reading the column of the table shows the number of years each employee has been working in the company.
    Here Insert Picture Description
输入以下命令以创建初始元胞数组 employees:

devel = {{'Lee','Reed','Hill'}, {'Dean','Frye'}, ...
   {'Lane','Fox','King'}};
sales = {{'Howe','Burns'}, {'Kirby','Ford'}, {'Hall'}};
mgmt = {{'Price'}, {'Clark','Shea'}, {'Sims'}};
qual = {{'Bates','Gray'}, {'Nash'}, {'Kay','Chase'}};
docu = {{'Lloyd','Young'}, {'Ryan','Hart','Roy'}, {'Marsh'}};

employees = [devel; sales; mgmt; qual; docu]
employees =

    {1x3 cell}    {1x2 cell}    {1x3 cell}
    {1x2 cell}    {1x2 cell}    {1x1 cell}
    {1x1 cell}    {1x2 cell}    {1x1 cell}
    {1x2 cell}    {1x1 cell}    {1x2 cell}
    {1x2 cell}    {1x3 cell}    {1x1 cell}

Here Insert Picture Description

Converting the cellular array along the dimension of the structure 1

  1. Cellular converter 5 × 3 array along a first dimension 3 × 1 to construct a structure having five fields. Cellular dimension along each row of the array 1 becomes a field in the array of structures:
    traversing a first dimension (i.e., vertical dimension), comprising five rows, each row header is as follows:

rowHeadings = {'development', 'sales', 'management', 'quality', 'documentation'};
Here Insert Picture Description

  1. The cellular array into dimension associated with this array of structures depts:
depts = cell2struct(employees, rowHeadings, 1)
depts =
3x1 struct array with fields:
    development
    sales
    management
    quality
    documentation
  1. With this line-oriented structure to find the name of the work the company has more than 10 years of development staff at:
depts(1:2).development
ans =
    'Lee'    'Reed'    'Hill'
ans =
    'Dean'    'Frye'

The same array into a cellular structure body 2 along a dimension

  1. Cellular converter 5 × 3 array along a second dimension to construct a 5 × 1 structure having three fields. Cellular array along the dimension of each column 2 of the structure into a field in the array:
    Here Insert Picture Description
  2. Along the second dimension (or horizontal dimension) traversing the cellular array. The column heading changes generated field structure:
colHeadings = {'fiveYears' 'tenYears' 'fifteenYears'};

years = cell2struct(employees, colHeadings, 2)
years =
5x1 struct array with fields:
    fiveYears
    tenYears
    fifteenYears
  1. When using the column to the body structure, it shows the number of employees have worked at least five years in the sales department and file.
[~, sales_5years, ~, ~, docu_5years] = years.fiveYears
sales_5years =
    'Howe'    'Burns'
docu_5years =
    'Lloyd'    'Young'

Converting only a portion of the cellular array structure is:

  1. Converting the first row only the cellular and the last row of the array. This will generate 3 × 1 array of structures having two fields:
rowHeadings = {'development', 'documentation'};

depts = cell2struct(employees([1,5],:), rowHeadings, 1)
depts =
3x1 struct array with fields:
    development
    documentation

Here Insert Picture Description

  1. Display belong to employees in these sectors for all three time periods:
for k=1:3
   depts(k,:)
end

ans =
      development: {'Lee'  'Reed'  'Hill'}
    documentation: {'Lloyd'  'Young'}
ans =
      development: {'Dean'  'Frye'}
    documentation: {'Ryan'  'Hart'  'Roy'}
ans =
      development: {'Lane'  'Fox'  'King'}
    documentation: {'Marsh'}

Guess you like

Origin www.cnblogs.com/cloud-ken/p/11785931.html