1、矩阵运算
点乘:对于两个同维度的向量,点乘结果是这两个向量对应分量的乘积之和。
点除:是指对两个数组的对应元素进行除法运算。
点幂:表示元素对元素的幂运算。
>> A=[1,2,3;4,5,6];
B=[1,1,1;2,2,2]>> D1=B.*AD1 =1     2     38    10    12>> D2=B./AD2 =1.0000    0.5000    0.33330.5000    0.4000    0.3333>> D3=B.^AD3 =1     1     116    32    642、变量查询
who:显示工作空间中的所有变量;
whos:查看工作空间中所有变量的详细属性;
>> who您的变量为:A   B   D1  D2  D3  >> whosName      Size            Bytes  Class     AttributesA         2x3                48  double              B         2x3                48  double              D1        2x3                48  double              D2        2x3                48  double              D3        2x3                48  double3、矩阵元素提取引用操作
A(i:j, m:n) 表示由矩阵 A 的第 i 到第 j 行和第 m 到第 n 列交叉线上的元素组成的子矩阵。可利用冒号提取矩阵 的整行或整列。
4、矩阵建立
利用函数建立数值矩阵,reshape函数用于建立数值矩阵,diag函数用于产生对角阵。
>> x=1:15x =1     2     3     4     5     6     7     8     9    10    11    12    13    14    15>> y=reshape(x,3,5)y =1     4     7    10    132     5     8    11    143     6     9    12    15>> z=1:5z =1     2     3     4     5>> diag(z)ans =1     0     0     0     00     2     0     0     00     0     3     0     00     0     0     4     00     0     0     0     5矩阵的基本运算:
(1) 矩阵转置('或transpose函数);
(2) 矩阵加和减;
(3) 矩阵乘法;
(4) 矩阵除法 A\b=inv(A)*b;
(5) 矩阵的乘方 a^2。
>> A=[1 2 3; 4 5 6; 7 8 9]A =1     2     34     5     67     8     9>> B=A'B =1     4     72     5     83     6     9>> B = transpose(A)B =1     4     72     5     83     6     95、数据转换
- 去除字符串\r\n\t等符号
rec_str = {"time": "2024-8-16","platformNum": 1,"platNull": 0}rec_str = strrep(rec_str, '\r', '');  % 去除\r
rec_str = strrep(rec_str, '\n', '');  % 去除\n
rec_str = strrep(rec_str, '\t', '');  % 去除\t- 将字符串转换成struct,提取key值对应的value值
jsonData = jsondecode(rec_str);
value = jsonData.platformNum;6、基于tcp实现服务端程序
% 创建Server Socket
s = tcpip('0.0.0.0', 30000, 'NetworkRole', 'Server','ByteOrder','littleEndian');
% 等待客户端连接
s.OutputBufferSize=100000;
disp('等待客户端连接...');
fopen(s);
disp('客户端已连接');while true    if s.BytesAvailable>0rec_data = fread(s, s.BytesAvailable);% 将接收到的数据转换为字符串rec_str = char(rec_data);disp(rec_str')rec_str = strrep(rec_str', '\r', '');  % 去除\rrec_str = strrep(rec_str, '\n', '');  % 去除\nrec_str = strrep(rec_str, '\t', '');  % 去除\t% 将字符串转换为结构体jsonData = jsondecode(rec_str);value = jsonData.platformNum;str = '{"name": "John", "age": 30, "city": "New York"}';%将字符串转换成json% json_obj = jsonencode(str);% disp(json_obj);fwrite(s, str);pause(0.1); % 防止密集轮询end
end
fclose(s);
7、HTTP通信
data = {'key1', 'value1', 'key2', 'value2'};
% 创建一个JSON对象
% jsonData = jsonencode(data);% 设置weboptions
options = weboptions('ContentType', 'json','Timeout', 100);% 上传JSON数据到指定URL
url = 'http://192.168.4.11:8080/4009'; % 替换为你的API端点
response = webwrite(url, data, options);% 输出响应
disp(response);if response.Status == 200disp("数据上传成功")elsedisp("数据上传失败")
end