(2).按下Shift+Enter即可换行。(用于希望先输入多条语句,然后再同时顺序执行它们)
MATLAB中的基本数据类型包括数值类型、逻辑类型、字符串、函数句柄、结构体和单元数组类型。
>> x = 5;
>> y = 2/5;
>> z = 0.2687;
>> m = 0;
>> whos
Name Size Bytes Class Attributes
m 1x1 8 double
x 1x1 8 double
y 1x1 8 double
z 1x1 8 double
(2).在MATLAB中,单精度浮点数类型不能与整数类型进行算术运算。
>> c = single(6.02);
>> d = 7.36;
>> n = int16(10);
>> result = c * n
错误使用 .*
整数只能与相同类的整数或标量双精度值组合使用。
>> result = d * n
result =
74
>> str = 'Hello world.';
>> nstr = str + 2.5
nstr =
74.5000 103.5000 110.5000 110.5000 113.5000 34.5000 121.5000 113.5000 116.5000 110.5000 102.5000 48.5000
(2).字符串内的单引号由两个连续的单引号来表示。
>> String = 'I''m a goog boy.'
String =
I'm a goog boy.
(3).可以直接将字符串用数组方式来实现连接。
>> A = 'Hell0 ';
>> B = 'world!';
>> C = [A, B]
C =
Hell0 world!
>> Student.name = 'Amy';
>> Student.age = '20';
>> Student.height = '168';
>> Student.weight = '55';
>> Student
Student =
name: 'Amy'
age: '20'
height: '168'
weight: '55'
>> newArray = struct('Day', {'Thursday','Friday'},'Time', {'15:00','21:00'},'Number', {12});
>> newArray
newArray =
1x2 struct array with fields:
Day
Time
Number
>> newArray[1]
newArray[1]
|
错误: 圆括号或方括号不对称或异常。
>> newArray(1)
ans =
Day: 'Thursday'
Time: '15:00'
Number: 12
>> newArray(2)
ans =
Day: 'Friday'
Time: '21:00'
Number: 12
>> Array = 4:10
Array =
4 5 6 7 8 9 10
需要设置步长时,只需在中间加一个参数即可,格式为:
>> Array = 1:2:20
Array =
1 3 5 7 9 11 13 15 17 19
也可以使用linspace函数来创建线性序列和逻辑序列。其应用的两种格式为
>> Array = linspace(1,11,11)
Array =
1 2 3 4 5 6 7 8 9 10 11
>> C = {'x', [1,3,6];10, pi}
C =
'x' [1x3 double]
[10] [ 3.1416]
(2).利用cell函数来创建空单元数组,其格式为:
>> a = cell(2,2)
a =
[] []
[] []
>> C{1, 1}
ans =
x
>> C(1, 1)
ans =
'x'
>> C = {'x', [1,3,6];10, pi}
C =
'x' [1x3 double]
[10] [ 3.1416]
>> D = {25; 'MATLAB'}
D =
[25]
'MATLAB'
>> T = {C D}
T =
{2x2 cell} {2x1 cell}
(2).单元数组中指定单元的删除,只需要将空矩阵赋值给该单元即可。
>> C = {'x', [1,3,6];10, pi};
>> X = reshape(C, 1, 3)
错误使用 reshape
要执行 RESHAPE,请勿更改元素数目。
>> X = reshape(C, 1, 4)
X =
'x' [10] [1x3 double] [3.1416]
>> mapObj = containers.Map({'小明', '小红'}, {15010230014, 15070150220})%姓名到学号的简单映射
mapObj =
Map (带属性):
Count: 2
KeyType: char
ValueType: double
>> mapObj.Count
ans =
2
>> mapObj.KeyType
ans =
char
>> mapObj.ValueType
ans =
double
>> mapObj = containers.Map({'小明', '小红', '小亮'}, {150102, 150707, 150209});
>> keys(mapObj)
ans =
'小明' '小红' '小亮'
>> values(mapObj)
ans =
[150102] [150707] [150209]
>> number_x = mapObj('小明')
number_x =
150102
>> numbers = values(mapObj, {'小红', '小亮'})
numbers =
[150707] [150209]
>> mapObj = containers.Map({'小明', '小红', '小亮'}, {150102, 150707, 150209});
>> remove(mapObj, '小亮')
ans =
Map (带属性):
Count: 2
KeyType: char
ValueType: double
>> mapObj('小刚') = 150104;
>> mapObj('小红') = 140108;
>> keys(mapObj)
ans =
'小明' '小刚' '小红'
>> values(mapObj)
ans =
[150102] [150104] [140108]