总结一下matlab的常用数据类型,消除对matlab的陌生感,方便以后查询。
Matlab中,向量可以视为一维数组,矩阵可以视为二维数组,维数≷2的都称为多维数组。
第三维称为”页”,第四维称为“箱”。
A =
1 2
3 4
>> Z = cat(1,A,A)#行并
Z =
1 2
3 4
1 2
3 4
>> Z = cat(2,A,A)#列并
Z =
1 2 1 2
3 4 3 4
可以将元胞数组看做一种无所不包的广义矩阵,其元素可以存储不同数据类型的数据。
区别于一般矩阵,元胞数组使用花括号”{}”创建。
元胞和元胞里的内容是两个不同范畴的东西,我理解为cell是一个指针数组,其元素一个指针,要想得到对应元素所指向的元素需要一层解引用。matlab中设计两种不同的操作:元胞外标识
和元胞内编址
。
A(2,4)指的是第2行第4列的元胞元素。
A{2,4}指的是元胞数组中第2行第4列的元胞内容。
>> cell_one = {1,'3','3'}
cell_one =
[1] '3' '3'
>> cell_one{2,3}=23 #改变元胞内容
cell_one =
[1] '3' '3'
[] [] [23]
>> cell_two = cell(2,2)
cell_two =
[] []
[] []
>> cell_two(1,2)={1}#改变元胞内容
cell_two =
[] [1]
[] []
>> cell_two{2,2}=3
cell_two =
[] [1]
[] [3]
类似于C++的结构体,结构是MATLAB提供的一种将选择的数据存储到一个实体的数据类型。用户可通过存储数据时指定的域名
来对域中的数据进行访问。
>> strct.name = 'one'
strct =
name: 'one'
>> strct.sex = 'f'
strct =
name: 'one'
sex: 'f'
>> strct(2).name = 'two'
strct =
1x2 struct array with fields:
name
sex
>> struc = struct('name','one','sex','f','moneys',[1,2,3])
struc =
name: 'one'
sex: 'f'
moneys: [1 2 3]
>>
需要注意的是,在结构数组的赋值过程中,如果需要将元胞数组赋值给结构数组需要用符号”{{}}”。
map容器是一个快速键查找数据结构,可以提供多种方法对其中的个体元素进行寻访。相当于python中的键值对,不同的是一个map的键的数据类型必须是一致的。
Map是Map类中的对象,有三个只读的默认属性。需要注意的是在示例化Map对象之后KeyType、和ValueType就不能修改,即一个Map能接受到的键值对的类型在创建Map时就固定了。
Map类的只读属性
属性 含义 Count 存储键值对的个数 KeyType 表示key的数据类型 ValueType 表示value的数据类型,两种以上时为any
> map_null = containers.Map()
map_null =
Map (具有属性):
Count: 0
KeyType: char
ValueType: any
>> map_null(1) = 1 % 空map的实例map_null的键应该是char
错误使用 containers.Map/subsasgn
指定的 key 类型与此容器所需的类型不匹配。
>> map_null('1') = 1
map_null =
Map (具有属性):
Count: 1
KeyType: char
ValueType: any
>> mounth = {'Jan','Feb'};v = {327,363};
>> rainMap = containers.Map(mounth,v)
rainMap =
Map (具有属性):
Count: 2
KeyType: char
ValueType: double
>> rainMap('g')='g'
错误使用 containers.Map/subsasgn
指定的 value 类型与此容器所需的类型不匹配。
>>
可以通过keys方法查看Map中的所有键,values方法查看Map中的所有值。返回的cell数组。
>> rainMap.keys()
ans =
'Feb' 'Jan' 'g'
>> rainMap.values()
ans =
[363] [327] [3]
>>
>> rainMap.keys()
ans =
'Feb' 'Jan' 'g'
>> rainMap.values()
ans =
[363] [327] [3]
>> rainMap(1)
错误使用 containers.Map/subsref
指定的 key 类型与此容器所需的类型不匹配。
>> rainMap('f')
错误使用 containers.Map/subsref
此容器中不存在指定的键。
>> rainMap.values({'Feb','Jan'})
ans =
[363] [327]
在Matlab中的类分为两种:句柄类
和实体值类
。Map类的对象的赋值运算,其实是在创建一个对象副本。
>> mapa = containers.Map([1,2],{'2',3})
mapa =
Map (具有属性):
Count: 2
KeyType: double
ValueType: any
>> mapb = mapa
mapb =
Map (具有属性):
Count: 2
KeyType: double
ValueType: any
>> mapb(3)='g'
mapb =
Map (具有属性):
Count: 3
KeyType: double
ValueType: any
>> mapa
mapa =
Map (具有属性):
Count: 3
KeyType: double
ValueType: any
类似于python中的DataFrame,但是每一列的数据不要求是同一类型,而且行名和列名不需要指定为数字。
>> name = {'tom';'jack'};
height = {177,160}';
tables = table(name,height,'rowname',{'1','22'});
>> tables
tables =
name height
______ ______
1 'tom' [177]
22 'jack' [160]
>> tables.Properties
ans =
Description: ''
VariableDescriptions: {}
VariableUnits: {}
DimensionNames: {'Row' 'Variable'}
UserData: []
RowNames: {2x1 cell}
VariableNames: {'name' 'height'}
>>
>> tables(:,'name') % 'name'列
ans =
name
______
1 'tom'
22 'jack'
>> tables(1,:) % 第1行
ans =
name height
_____ ______
1 'tom' [177]
>> tables('22',:) % '1'行
ans =
name height
______ ______
22 'jack' [160]
>> tables(:,1:2)
ans =
name height
______ ______
1 'tom' [177]
22 'jack' [160]
>> tables.height
ans =
[177]
[160]