MATLAB3-1变量-台大郭彦甫

目录

数据类型

数据类型转换

字符char

ASCII码表

字符串string

逻辑运算和赋值

question

strcmp函数

exercise

结构体structure

结构体相关函数

[嵌套结构nesting structures](#嵌套结构nesting structures)

[元胞数组Cell Array](#元胞数组Cell Array)

why

Exercise

[读取cell array](#读取cell array)

问题:

[cell array functions](#cell array functions)

[num2cell() and mat2cell()](#num2cell() and mat2cell())

[多维数组Multidimensional Array](#多维数组Multidimensional Array)

reshape

检查variable


数据类型

常见的数据类型有逻辑logical、字符char、数值numeric、元胞cell、结构体struct

数据类型转换

字符char

运行代码:

Matlab 复制代码
s1 = 'h'
whos
uint16(s1)

s2 = 'H'
whos
uint16(s2)

得到:

ASCII码表

二进制 八进制 十进制 十六进制 字符/缩写 解释
00000000 000 0 00 NUL (NULL) 空字符
00000001 001 1 01 SOH (Start Of Headling) 标题开始
00000010 002 2 02 STX (Start Of Text) 正文开始
00000011 003 3 03 ETX (End Of Text) 正文结束
00000100 004 4 04 EOT (End Of Transmission) 传输结束
00000101 005 5 05 ENQ (Enquiry) 请求
00000110 006 6 06 ACK (Acknowledge) 回应/响应/收到通知
00000111 007 7 07 BEL (Bell) 响铃
00001000 010 8 08 BS (Backspace) 退格
00001001 011 9 09 HT (Horizontal Tab) 水平制表符
00001010 012 10 0A LF/NL(Line Feed/New Line) 换行键
00001011 013 11 0B VT (Vertical Tab) 垂直制表符
00001100 014 12 0C FF/NP (Form Feed/New Page) 换页键
00001101 015 13 0D CR (Carriage Return) 回车键
00001110 016 14 0E SO (Shift Out) 不用切换
00001111 017 15 0F SI (Shift In) 启用切换
00010000 020 16 10 DLE (Data Link Escape) 数据链路转义
00010001 021 17 11 DC1/XON (Device Control 1/Transmission On) 设备控制1/传输开始
00010010 022 18 12 DC2 (Device Control 2) 设备控制2
00010011 023 19 13 DC3/XOFF (Device Control 3/Transmission Off) 设备控制3/传输中断
00010100 024 20 14 DC4 (Device Control 4) 设备控制4
00010101 025 21 15 NAK (Negative Acknowledge) 无响应/非正常响应/拒绝接收
00010110 026 22 16 SYN (Synchronous Idle) 同步空闲
00010111 027 23 17 ETB (End of Transmission Block) 传输块结束/块传输终止
00011000 030 24 18 CAN (Cancel) 取消
00011001 031 25 19 EM (End of Medium) 已到介质末端/介质存储已满/介质中断
00011010 032 26 1A SUB (Substitute) 替补/替换
00011011 033 27 1B ESC (Escape) 逃离/取消
00011100 034 28 1C FS (File Separator) 文件分割符
00011101 035 29 1D GS (Group Separator) 组分隔符/分组符
00011110 036 30 1E RS (Record Separator) 记录分离符
00011111 037 31 1F US (Unit Separator) 单元分隔符
00100000 040 32 20 (Space) 空格
00100001 041 33 21 !
00100010 042 34 22 "
00100011 043 35 23 #
00100100 044 36 24 $
00100101 045 37 25 %
00100110 046 38 26 &
00100111 047 39 27 '
00101000 050 40 28 (
00101001 051 41 29 )
00101010 052 42 2A *
00101011 053 43 2B +
00101100 054 44 2C ,
00101101 055 45 2D -
00101110 056 46 2E .
00101111 057 47 2F /
00110000 060 48 30 0
00110001 061 49 31 1
00110010 062 50 32 2
00110011 063 51 33 3
00110100 064 52 34 4
00110101 065 53 35 5
00110110 066 54 36 6
00110111 067 55 37 7
00111000 070 56 38 8
00111001 071 57 39 9
00111010 072 58 3A :
00111011 073 59 3B ;
00111100 074 60 3C <
00111101 075 61 3D =
00111110 076 62 3E >
00111111 077 63 3F ?
01000000 100 64 40 @
01000001 101 65 41 A
01000010 102 66 42 B
01000011 103 67 43 C
01000100 104 68 44 D
01000101 105 69 45 E
01000110 106 70 46 F
01000111 107 71 47 G
01001000 110 72 48 H
01001001 111 73 49 I
01001010 112 74 4A J
01001011 113 75 4B K
01001100 114 76 4C L
01001101 115 77 4D M
01001110 116 78 4E N
01001111 117 79 4F O
01010000 120 80 50 P
01010001 121 81 51 Q
01010010 122 82 52 R
01010011 123 83 53 S
01010100 124 84 54 T
01010101 125 85 55 U
01010110 126 86 56 V
01010111 127 87 57 W
01011000 130 88 58 X
01011001 131 89 59 Y
01011010 132 90 5A Z
01011011 133 91 5B [
01011100 134 92 5C \
01011101 135 93 5D ]
01011110 136 94 5E ^
01011111 137 95 5F _
01100000 140 96 60 `
01100001 141 97 61 a
01100010 142 98 62 b
01100011 143 99 63 c
01100100 144 100 64 d
01100101 145 101 65 e
01100110 146 102 66 f
01100111 147 103 67 g
01101000 150 104 68 h
01101001 151 105 69 i
01101010 152 106 6A j
01101011 153 107 6B k
01101100 154 108 6C l
01101101 155 109 6D m
01101110 156 110 6E n
01101111 157 111 6F o
01110000 160 112 70 p
01110001 161 113 71 q
01110010 162 114 72 r
01110011 163 115 73 s
01110100 164 116 74 t
01110101 165 117 75 u
01110110 166 118 76 v
01110111 167 119 77 w
01111000 170 120 78 x
01111001 171 121 79 y
01111010 172 122 7A z
01111011 173 123 7B {
01111100 174 124 7C |
01111101 175 125 7D }
01111110 176 126 7E ~
01111111 177 127 7F DEL (Delete) 删除

字符串string

多个字符就是字符串string

Matlab 复制代码
s1 = 'Example'
s2 = 'String'
s3 = [s1 s2]

结果:

但是如果输入以下代码会报错:

Matlab 复制代码
s4 = [s1;s2]

原因是s1 example有7个char,而string有6个char,矩阵肯定放不进去的

逻辑运算和赋值

很多数值和逻辑运算能够应用到字符串中

Matlab 复制代码
str = 'aardvark'
'a' == str
str(str == 'a') = 'Z'

'a'==str是判断字符串是否与str中的每个char一致,所以str中char=='a'的输出1,反之输出2

最后一行代码,意思为将str中char是a的char更改为Z

所以结果为:

question

what if we want to compare the entire string with another?

意思就是怎么判断整个string字符串是不是和str一致

strcmp函数

strcmp(s1,s2), 比较 s1s2,如果二者相同,则返回 1 (true),否则返回 0 (false)。如果文本的大小和内容相同,则它们将视为相等。返回结果的数据类型为 logical

Matlab 复制代码
strcmp(str,'ZZrdvZrk')

exercise

我的代码:

Matlab 复制代码
s1 = 'I like the letter E'
lth = strlength(s1)
for i = 1:lth
    s2(i)=s1(lth+1-i);
end
s2

结果没问题:

结构体structure

Matlab 复制代码
student.name = 'Jon Doe';
student.id = 'jd2@sfu.ca';
student.number = 301073268;
student.grade = [100 75 73;95 91 85.5;100 98 72];
student

结果:

如果想再加入第二个同学,怎么办?

Matlab 复制代码
student.name = 'Jon Doe';
student.id = 'jd2@sfu.ca';
student.number = 301073268;
student.grade = [100 75 73;95 91 85.5;100 98 72];

student(2).name = 'Ann Lane';
student(2).id = 'aln4%sfu.ca';
student(2).number = 301078853;
student(2).grade = [95 100 90;95 82 97;100 85 100];
student(1),student(2)

结果:

结构体相关函数

Matlab 复制代码
%% 结构体
student.name = 'Jon Doe';
student.id = 'jd2@sfu.ca';
student.number = 301073268;
student.grade = [100 75 73;95 91 85.5;100 98 72];

student(2).name = 'Ann Lane';
student(2).id = 'aln4%sfu.ca';
student(2).number = 301078853;
student(2).grade = [95 100 90;95 82 97;100 85 100];
student(2).grade(7)

fieldnames(student)
rmfield(student,'id')

结果:

id就被删除了(remove)--rmfield

嵌套结构nesting structures

Matlab 复制代码
%% nesting structures
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];
A.nest

元胞数组Cell Array

矩阵中不仅仅是数值------元胞数组

Matlab 复制代码
%% cell array
%method 1
clc;clear all;
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};
%method 2
clc;clear all;
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;

why

Exercise

Matlab 复制代码
%% exercise
B{1,1} = 'This is the first cell';
B{1,2} = [5+6j 4+5j];
B{2,1} = [1 2 3;4 5 6;7 8 9];
B{2,2} = {'Tim','Chirs'}

读取cell array

问题:

很简单就是:A{1,1}(1,1)------先把第一行第一列的array提取,在提取array中第一行第一列的1

cell array functions

num2cell() and mat2cell()

Matlab 复制代码
%% cell array functions
a = magic(3)
b = num2cell(a)
c = mat2cell(a,[1 1 1],3)%a row column

多维数组Multidimensional Array

下图 先把A建立起来,再把B建立起来,最后通过concatenation把A和B联结起来

Matlab 复制代码
%% multidimensional Array
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)

reshape

r1*c1必须等于==r2*c2

Matlab 复制代码
A = {'James Bond',[1 2;3 4;5 6];pi,magic(5)};
C = reshape(A,1,4)

检查variable

相关推荐
量子炒饭大师2 小时前
收集飞花令碎片——C语言关键字typedef
c语言·c++·算法
澡点睡觉2 小时前
【数据结构与算法Trip第4站】摩尔投票法
算法
zhangfeng11332 小时前
在 R 语言里,`$` 只有一个作用 按名字提取“列表型”对象里的单个元素 对象 $ 名字
开发语言·windows·r语言
Naiva2 小时前
ESP32-C3 入门09:基于 ESP-IDF + LVGL + ST7789 的 1.54寸 WiFi 时钟(SquareLine Studio 移植)
ide·笔记·vscode
文弱书生6563 小时前
5.后台运行设置和包设计与实现
服务器·开发语言·c#
编码浪子3 小时前
趣味学RUST基础篇(异步补充)
开发语言·后端·rust
..过云雨3 小时前
03.【Linux系统编程】基础开发工具1(yum软件安装、vim编辑器、编辑器gcc/g++)
linux·c语言·笔记·学习
songroom3 小时前
Rust : 关于Deref
开发语言·后端·rust
qq_401700413 小时前
QT子线程与GUI线程安全交互
开发语言·qt