台湾国立大学郭彦甫Matlab教程笔记(7)
today:
1.varibles:string,structure,cell 今天进阶的varibles
2.data access
最常用的是 numeric 下面是 double single integer
还有 logical,一般在 condition中
在matlab中当作函数使用。
int8:integer 8bit
uint16:unsigned integer 16
A=20; B=int8(A);就是把double的A转换成integer整数 八位的B
什么是 character ,在引号里面只有一个字源的时候 是character
1.a character is represented in ASCII using a numeric code between 0 to 255
2.create a character or a string by putting them into a pair of apostrophe撇号,引号
s1='h'
whos
uint16(s1)
在记忆体RAM中,不是存具体的 char,而是存放 ASCII code,如下图
1.an array collects characters:多个字元
s1='example';
s2='string';
2.string concatenation; 字符串的扩充
s3=[s1 s2];
s4=[s1;s2];
得到的是s3=[s1 s2];字符串连接,中间没有空格
s4=[s1;s2];的结果,报错,原因是 s1和s2长度不一致,没法形成一个新矩阵
换成长度一样的字符串便可以使用
logical operations and assignmens
1.many numerical and logical operators can be applied to strings
str='aardvark';
'a'==str
try this :
str(str=='a')='z'
str(3)的结果是什么? 小括号是indexing ,是位置,所以 str(3)=‘r’
‘a’==str 是str中8个位置一个一个的做逻辑运算,判断是否等于a,结果是 11000100
str(str==‘a’)='z’这句话怎么理解?
str==‘a’ 的结果是 11000100,8个indexing,现在它作为str的input ,即str(11000100)=‘z’,注意这里是赋值等号,所以 str中 第一个、第二个,第六个(为1的位置)赋值为Z
之前 都是 element 之间比较
question :what if we want to compare the entire string with another?
strcmp() 函数的基本功能是比较两个字符串是否相等
题目:给一个字符串,反向输出
分析:需要indexing ,
第一步,我需要 s1字符串长度 ,查找资料
下面想赋值,结果不起作用
原因 这里s2(l1+1-i)中 l1+1-i需要加括号,作为索引。变成 s2((l1+1-i))=s1(i);
源代码:这是我写的一个函数function,可以执行
function invertedOutput
s1='I like the letter E';
length=strlength(s1);
s2=blanks(length);
for i=1:length
s2(i)=s1((length+1-i));
end
disp(s1)
disp(s2)
1.a method of storing heterogeneous 由很多种类构成的,异质的 data
2.structures contain arrays called fields
3.student assignment grades:
例程:
student.name='John Doe';
student.id='[email protected]';
student.number =301073268;
student.grade =[100,75,73;...
95,91,85.5;
100,98,72];
既然是一个 varible 就可以做indexing ,用小括号.student(2)就表示第二个学生,student(3)就表示第三个,以此类推,可以不断扩充 add information to a structure
例程:
student(2).name='Ann Lane';
student(2).id='[email protected]';
student(2).number=301078853;
student(2).grade=[95 100 90; 95 82 97; 100 85 100];
exercise: retrieve the 3rd grade for Ann Lane
我的练习结果:首先找到Ann这个学生,然后找到她的成绩grade,成绩是一个矩阵,我用的是位置法找到第三个(第一行第三列)。
field 是结构体变量名称,比如name 或者 id 或者 grade 都是 field
try:
fieldnames(student)
rmfield(student,‘id’)
得到 student 这个结构体中有几个变量 ,回传的是 cell 。 然后删除掉名字为id 的这个变量
一个structure 里面可以嵌套别的 structure
also called embedded structure
举例子:
例程:
创建一个 嵌套结构体
A=struct('data',[3 4 7;8 0 1],'nest',...
struct('testnum','Test 1',...
'xdata',[4 2 8],'ydata',[7 1 6]));
结构体中增加一组成员
A(2).data=[9 3 2;7 6 5];
A(2).nest.testnum='Test 2';
A(2).nest.xdata=[3 4 2];
A(2).nest.ydata=[5 0 9];
显示两个成员中的nest变量内容
A.nest
看一下执行的结果,先只是输入A这个structure
查看A.data的内容
查看 A.nest的内容
再添加一个structure
1.another method of storing heterogeneous data
2.similar to matirx but each entry contains different type of data
3.declared using {}宣告的话用{}大括号
第一种宣告 前面用小括号指定位置,变量用大括号
A(1,1)={[1 4 3;0 5 8; 7 2 9]};
A(1,2)={‘Anne Smith’};
A(2,1)={3+7i};
A(2,2)={-pi:pi:pi};
A
A(1,1)={[1 4 3;0 5 8; 7 2 9]};
A(1,2)={'Anne Smith'};
A(2,1)={3+7i};
A(2,2)={-pi:pi:pi};
A
执行的结果:
第二种宣告:前面用大括号指定位置
A{1,1}=[1 4 3; 0 5 8; 7 2 9];
A{1,2}=‘Anne Smith’;
A{2,1}=3+7i;
A{2,2}=-pi:pi:pi;
A
A{1,1}=[1 4 3; 0 5 8; 7 2 9];
A{1,2}='Anne Smith';
A{2,1}=3+7i;
A{2,2}=-pi:pi:pi;
A
how does matlab do it?
1.each entry in a cell array holds a pointer to a data structure 这个cell中的每一个元素都是一个指针,这些指针指向不同的结构体
2.different cells of the same cell array can point to different types of data structures
cell的练习题
题目:创建一个cell名称为B,B的结构如图所示。
我的练习:
B{1,1}='This is the first cell';
B{1,2}=[5+6i 4+5i];
B{2,1}=[1 2 3;4 5 6;7 8 9];
B{2,2}=['Tim','Chris'];
celldisp(B)
运行结果:
accessing cell array
怎么读取cell呢?
首先,小括号indexing 指的是 指针指的是什么/
想看内容是什么需要 用大括号。
1.curly braces ,{},大括号, are used to access the “content” of cell arrays
question : what are the difference between C and D?
C=A{1,1}
D=A(1,1)
answer:小括号对cell而言指的是指针,这个pointer指向的是什么 ;大括号对cell而言指的是 具体的内容
question2:how do I get this number?
这个问题 就是现在得到了一个矩阵,求矩阵的第一个位置的数。很简单
A{1,1}(1,1)
cell array functions
herotenious data 有两种:cell 和structure
多讲讲怎样把matrix 转换成为 cell
function:num2cell() and mat2cell()
1.transform a matrix into a cell varible
第一 num2cell 这个matrix中每一个数字都需要独立成cell中的entry
example code
a=magic(3)
b=num2cell(a)
前两行代码执行后可以看到 b变成了 cell
另一个指令 是 entry grouping
c=mat2cell(a,[1 1 1],3)
意思是 a 分成 【1 1 1】 三个row, 每一个row容纳3的column
运行结果:
multidimensional array
vetor:one dimension
matrix:two dimension
array:three dimension :row .column.layer
假设是222
A{1,1,1}=[1 2 ;4 5];
A{1,2,1}=‘name’;
A{2,1,1}=2-4i;
A{2,1,2}=7;
A{1,1,2}=‘name2’;
A{1,2,2}=3;
A{2,1,2}=0:1:3;
A{2,2,2}=[4 5];
运行结果
但是这种方法太繁琐,不常用。
常用下面的函数:
cat()
这不是猫cat,而是 concatenation的简写,concatenation是扩充意思
A=[1 2;3 4];
B=[5 6;7 8];
cat的第一个参数表示方向,=1表示行,=2表示列,=3表示层layer
现在知道了concatenation 这个指令,怎么去建 array
22的matrix 然后 22的matrix 再用cat建层,这样很直观
代码:
A{1,1}=[1 2;4 5];
A{1,2}='name';
A{2,1}=2-4i;
A{2,2}=7;
B{1,1}='name2';
B{1,2}=3;
B{2,1}=0:1:3;
B{2,2}=[4 5]';
C=cat(3,A,B)
运行结果:这样也搭建起来三维的array,在数据量很大的时候这种方法有用
再来一个指令 reshape()
1.return a new array with assigned rows and columns要求行乘以列相等
举例:
A={‘James Bond’,[1 2;3 4;5 6]; pi,magic(5)}
C=reshape(A,1,4)
运行结果:
作业小练习:
题目:给定一个矩阵A,使用reshape函数把A变成矩阵B
A=[1:3;4:6];
B=reshape(A,3,2)
【总结一下】
本次笔记掌握了 matlab基本数据类型的进阶1,string和struct结构体。以及一些具体的应用。包括布置的作业题也完成输出在博客上。
后面补充了 cell 的一些用法。
到此,matlab的数据类型基本上记录完成了。