【Matlab基础】矩阵、绘图、函数计算与数据读取

  • 基础及预设置
  • 矩阵
  • 各种函数
  • 二维制图
  • 三维制图
  • 运算
  • 输入/输出
  • 各种语句
  • 数据读出/写入



这里用的是Matlab2016a版本

基础及预设置

1.设置路径

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据


选择路径,或是将文件拖动到当前目录下

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_02


2.布局及预设设置(这个看心情是否要设置)

python读matlab矩阵数据 matlab如何读取矩阵_数组_03


python读matlab矩阵数据 matlab如何读取矩阵_数学建模_04


3.基础工具栏(主页、绘图、APP)

python读matlab矩阵数据 matlab如何读取矩阵_数组_05


4.查看帮助

python读matlab矩阵数据 matlab如何读取矩阵_数据_06


进入帮助菜单或是运用doc、help函数

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_07


python读matlab矩阵数据 matlab如何读取矩阵_Matlab_08


5.清除

清除变量命令:

python读matlab矩阵数据 matlab如何读取矩阵_数据_09


清除界面命令:clc6.数据类型

python读matlab矩阵数据 matlab如何读取矩阵_数组_10


7.常用标点

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_11


python读matlab矩阵数据 matlab如何读取矩阵_数组_12


8.常用操作键

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_13


9.注意

a.

回车键直接运行显示结果,分号代表运行但不显示结果

b.

变量名的第1个字符必须是字母,后面可以跟字母、数字和下划线的任何组合。变量名有大小写区分,所以A和a是不同的变量。

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_14

矩阵

1.矩阵的创建

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_15


python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_16


linspace(起点,终点,数的个数(默认为100))

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_17


双击左边的变量也可以直接赋值

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_18


2.矩阵的函数创建

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_19


五阶希尔伯特矩阵

python读matlab矩阵数据 matlab如何读取矩阵_数组_20


四/五 阶幻方矩阵

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_21


python读matlab矩阵数据 matlab如何读取矩阵_Matlab_22


python读matlab矩阵数据 matlab如何读取矩阵_Matlab_23


4.矩阵运算

加 +

减 -

乘 *

除 /

幂 ^n

转置 ’

A\B 等价于 A-1B
A/B 等价于 A
B-15.裁剪与拼接

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_24


python读matlab矩阵数据 matlab如何读取矩阵_数学建模_25

各种函数

1.先来一大堆内部函数

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_26


2.常用指令函数

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_27


3.运算函数

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_28


反三角函数:asin,acos…

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_29

4.矩阵函数

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_30


isempty(x)判断是否为空矩阵[ ],返回值为0或1

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_31


x=find(关系表达式)

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_32


5.向量(有限数组)函数

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_33


6.工作区命令函数

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_34


python读matlab矩阵数据 matlab如何读取矩阵_Matlab_35


python读matlab矩阵数据 matlab如何读取矩阵_数组_36


7.自定义函数

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_37


python读matlab矩阵数据 matlab如何读取矩阵_数组_38


python读matlab矩阵数据 matlab如何读取矩阵_数据_39


python读matlab矩阵数据 matlab如何读取矩阵_数学建模_40

二维制图

三维制图

1.figure函数建图

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_41


2.mesh制图

python读matlab矩阵数据 matlab如何读取矩阵_数组_42


先生成网格图(meshgrid),在输入z的方程,然后制图

3.surf制图

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_43

python读matlab矩阵数据 matlab如何读取矩阵_数据_44

运算

1.计算运算符

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_45


矩阵运算不用.*矢量需要用

2.逻辑运算符
& 逻辑与(且),| 逻辑或,~ 逻辑非,xor 异或
a&b,and(a,b) 只有当a和b都为1时才为1,其他情况为0
a|b,or(a,b) 只有当a和b都为0时才为0,其他情况为1
~a,not(a) 见1为0,见0为1
xor(a,b) a和b相同时为0,不同时为1

3.数组运算

数组与标量

a=[a1,a2,…an], c为标量

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_46


1到4可直接乘除

5和6中 . 不可缺少数组与数组

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_47


4.比较

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_48

2.取余rem/mod

输入/输出

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_49


disp输出

python读matlab矩阵数据 matlab如何读取矩阵_python读matlab矩阵数据_50


sprintf输出

python读matlab矩阵数据 matlab如何读取矩阵_Matlab_51


python读matlab矩阵数据 matlab如何读取矩阵_Matlab_52

各种语句

1.循环语句
for循环:
for i=循环范围
循环体
end

while循环:
while 关系表达式
命令集合
end

2.条件/判断 语句
if 关系表达式
命令集合1
elseif 关系表达式
命令集合 2
…………
elseif 关系表达式
命令集合 n
else
命令集合n+1
end

选择语句switch
格式为:switch 表达式
case 表达式1
语句组1
case 表达式2
语句组2
……
case 表达式m
语句组m
otherwise
语句组m+1
end

try 试探语句

格式为:try

语句组1

catch

语句组2

end

try语句先试探性执行语句组1,如果语句组1在执行过程中出现错误,则将错误信息赋给保留的lasterr变量,并转去执行语句组2。这种试探性执行语句是其他高级语言所没有的

python读matlab矩阵数据 matlab如何读取矩阵_数据_53


错误语句

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_54

数据读出/写入

xlsx文件

m=magic(5);                          %生成一个5X5矩阵
file_name = 'text1.xlsx';            %命个名
xlswrite(file_name,m,'data','B2');   %将数据写入到表格中的data工作表下,从B2开始
m_=xlsread(file_name,'data','B2:F6') %读出数据到Matlab

python读matlab矩阵数据 matlab如何读取矩阵_数学建模_55

csv文件

txt文件/mat文件
load a.txt
load a.mat

save

图像文件

im = imreadfilename)          %读取图片数据v
imwrite(im,filename,fmt)     %写入图片数据到硬盘