matlab中数据结构之-tables

  作为matlab中的一种数据结构,是一种按行和列存储信息的一种表格式的数据结构,同一列中的数据具有相同的长度,和我们平常见到的表差不多。使用关键table函数创建,格式为tableName = table(2thCol, 3thCol, ..., 'RowNames', 1thCol),当然需要提前准备一些数据。

请看:

 1 >> names = ["Harry"; "Sally"; "Jose"]
 2 names = 
 3   3×1 string array
 4     "Harry"
 5     "Sally"
 6     "Jose"
 7 >> weights = [185; 133; 210]
 8 weights =
 9    185
10    133
11    210
12 >> heights = [74; 65.4; 72.2]
13 heights =
14    74.0000
15    65.4000
16    72.2000
17 >> patients = table(weights, heights, 'RowNames', names)
18 patients =
19   3×2 table
20              weights    heights
21              _______    _______
22     Harry      185         74  
23     Sally      133       65.4  
24     Jose       210       72.2  

第1行创建names列向量

第7行创建weights列向量

第12行创建heights列向量

第17行创建table,表中必须明确指定行表头,并用'RowNames'关键字,提前说明。

table和矩阵的使用差不多,也有自己的特殊方便之处,请看

>> patients(:,1)
ans =
  3×1 table
             weights
             _______
    Harry      185  
    Sally      133  
    Jose       210  
>> patients(:, 'weights')
ans =
  3×1 table
             weights
             _______
    Harry      185  
    Sally      133  
    Jose       210  
>> patients({'Harry' 'Jose'}, :)
ans =
  2×2 table
             weights    heights
             _______    _______
    Harry      185         74  
    Jose       210       72.2  
>> patients(1:2:3, :)
ans =
  2×2 table
             weights    heights
             _______    _______
    Harry      185         74  
    Jose       210       72.2 

怎么样,你是学会了,还是学废了,务必好好理解。

posted @ 2022-08-27 09:04  叕叒双又  阅读(890)  评论(0)    收藏  举报