Task 05: structure array

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45138230/article/details/102727977

Array of structures

Here Insert Picture Description

create

Example table at 3.26 as student records, an array called student, trial structure array representation
Here Insert Picture Description

student(1).ID = 101;
student(2).ID = 102;
student(1).name = 'wang';
student(2).name = 'chang';
student(1).age = 21;
student(2).age = 20;
struct(student)

>> practice
ans = 
  包含以下字段的 1×2 struct 数组:
    ID
    name
    age

The cell array structure array conversion

Here Insert Picture Description
Here Insert Picture Description

S = struct('category', 'tree', 'heigh', '28.5', 'name', 'brich')
C = struct2cell(S)
size(C)

>> practice
S = 
  包含以下字段的 struct:

    category: 'tree'
       heigh: '28.5'
        name: 'brich'
C =
  3×1 cell 数组
    {'tree' }
    {'28.5' }
    {'brich'}
ans =
     3     1

Example 3.28 Example 3.26, linking, converted into an array of structures student cell array C

student(1).ID = 101;
student(2).ID = 102;
student(1).name = 'wang';
student(2).name = 'chang';
student(1).age = 21;
student(2).age = 20;
struct(student)
C = struct2cell(student);
C = [C(:,1), C(:,2)]

>> practice
ans = 
  包含以下字段的 1×2 struct 数组:
    ID
    name
    age
C =
  3×2 cell 数组
    {[ 101]}    {[  102]}
    {'wang'}    {'chang'}
    {[  21]}    {[   20]}

The data processing unit of the array

3.29 Example of averaging 88,95,79,73

C{1,1} = 88;
C{1,2} = 95;
C{1,3} = 79;
C{1,4} = 73;
s = 0;
for i = 1 : 4
    s(i) = C{1,i};
end, s, mean(s)

>> practice
s =
    88    95    79    73
ans =
   83.7500

Guess you like

Origin blog.csdn.net/weixin_45138230/article/details/102727977