matlab基础学习——数组
2015-09-09 20:51
561 查看
一、数组的四则元素
数组运算时,运算符需加上点号,即:“.-”、“.+”
>> a=[1 2 3;4 5 6;7 8 9]
a =
1 2 3
4 5 6
7 8 9
>> b=[1 1 1;2 2 2;3 3 3]
b =
1 1 1
2 2 2
3 3 3
>> a./b
ans =
1.0000 2.0000 3.0000
2.0000 2.5000 3.0000
2.3333 2.6667 3.0000
>> a/b
Warning: Matrix is singular to working precision.
ans =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
>> a.*b
ans =
1 2 3
8 10 12
21 24 27
>> a*b
ans =
14 14 14
32 32 32
50 50 50
数组与数的运算:常数写在前,点号运算符号,数组写在后
>> 3+b
ans =
4 4 4
5 5 5
6 6 6
>> 3.+b
ans =
4 4 4
5 5 5
6 6 6
>> b.+3
??? b.+3
|
Error: Unexpected MATLAB operator.
>> a^3
ans =
468 576 684
1062 1305 1548
1656 2034 2412
>> a.^3
ans =
1 8 27
64 125 216
343 512 729
指数运算,exp
>> exp(a)
ans =
1.0e+003 *
0.0027 0.0074 0.0201
0.0546 0.1484 0.4034
1.0966 2.9810 8.1031
>> log(a)
ans =
0 0.6931 1.0986
1.3863 1.6094 1.7918
1.9459 2.0794 2.1972
>> sqrt(a)
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
数组寻址
>> a=rand(1,10)
a =
0.0782 0.4427 0.1067 0.9619 0.0046 0.7749 0.8173 0.8687 0.0844 0.3998
>> a(4)
ans =
0.9619
>> a(2:6)
ans =
0.4427 0.1067 0.9619 0.0046 0.7749
数组排序
>> sort(a)
ans =
0.0046 0.0782 0.0844 0.1067 0.3998 0.4427 0.7749 0.8173 0.8687 0.9619
二、字符串
a、用单引号引用
>> str='this is a string'
str =
this is a string
>> whos
Name Size Bytes Class Attributes
a 2x3 48 double
ans 1x10 80 double
b 3x3 72 double
d 3x3 72 double
e 3x3 72 double
l 3x3 72 double
p 3x3 72 double
q 3x3 72 double
r 3x3 72 double
str 1x16 32 char
u 3x3 72 double
v 1x4 32 double
>> whos str
Name Size Bytes Class Attributes
str 1x16 32 char
>> string1='this'
string1 =
this
>> string2='is'
string2 =
is
>> str=[string1,string2]
str =
thisis
>> str(3)
ans =
i
>> str(2:4)
ans =
his
b、字符串变为ASCII码。用abs函数
>> str='this is bed'
str =
this is bed
>> a=abs(str)
a =
116 104 105 115 32 105 115 32 98 101 100
c、ASCII变字符串,char函数
>> char(a)
ans =
this is bed
d、字符串比较,strcmp
>> str1='this'
str1 =
this
>> str2='this '
str2 =
this
>> strcmp(str1,str2)
ans =
0
>> str3='this'
str3 =
this
>> strcmp(str1,str3)
ans =
1
字符串相等为1,不等为0
其中的某个字符比较,strncmp
>> strcmp(str1,str3)
ans =
1
>> strncmp(str1,str2,3)
ans =
1
e、字符的运算比较
== 等于 eq
~=不等于ne
<小于lt
>大于gt
<=小于等于le
>=大于等于ge
>> str1='matlab'
str1 =
matlab
>> str2='MATLAB'
str2 =
MATLAB
>> str1<str2
ans =
0 0 0 0 0 0
>> str1>str2
ans =
1 1 1 1 1 1 说明小写字母对应的ASCII码的值比大写字母的大
f、字符串的查找与替换
findstr、strfind、strrep
>> str='this is a book'
str =
this is a book
>> strfind(str,'s') 查找字符‘s',出现在第4和7的位置
ans =
4 7
g、字符串与数值的转换
hex2dec 将16进制字符转换成10进制整数
dec2hex 将10进制整数转换成16进制字符串
bin2dec 将2进制字符转换成10进制整数
dec2bin 将10进制整数转换成2进制字符串
base2dec 将B底字符串转换成10进制整数
hex2num 将16进制
upper
lower
fprintf
sprintf
sscanf
char
num2str
int2str
数组与字符的转换
str=mat2str(mat)将二维矩阵mat转换成一个字符串str
h、多项式
1、多项式生成
直接输入系数法
>> p=[2 3 4 5 6 0 7] 定义系数项
p =
2 3 4 5 6 0 7
>> poly2sym(p)
ans =
2*x^6 + 3*x^5 + 4*x^4 + 5*x^3 + 6*x^2 + 7
特征多项式输入法
>> a=[1 2 3 4;5 6 7 8;8 7 6 5;4 3 2 1]
a =
1 2 3 4
5 6 7 8
8 7 6 5
4 3 2 1
>> poly(a)
ans =
1.0000 -14.0000 -72.0000 0.0000 0.0000
>> poly2sym(poly(a))
ans =
x^4 - 14*x^3 - 72*x^2 + (3460367834469499*x)/39614081257132168796771975168 + 2691467142043917/44601490397061246283071436545296723011960832
由多项式的根逆推多项式
>> root=[-4 -2+2i -2-2i 5]
root =
-4.0000 -2.0000 + 2.0000i -2.0000 - 2.0000i 5.0000
>> p=poly(root)
p =
1 3 -16 -88 -160
>> disp(poly2sym(p))
x^4 + 3*x^3 - 16*x^2 - 88*x - 160
2、多项式求值,polyval数组的计算求值、polyvalm矩阵计算求值
>> p=[1 -20 -16 480 98]
p =
1 -20 -16 480 98
>> x=4
x =
4
>> polyval(p,x) 求x=4时,多项式的值
ans =
738
3、求多项式的根
>> roots(p)
ans =
19.5494
5.3052
-4.6514
-0.2031
4、多项式四则运算
加减法
>> a=[2 3 4 5]
a =
2 3 4 5
>> b=[4 5 6 7]
b =
4 5 6 7
>> a+b
ans =
6 8 10 12
>> y1=poly2sym(a)
y1 =
2*x^3 + 3*x^2 + 4*x + 5
>> y2=poly2sym(b)
y2 =
4*x^3 + 5*x^2 + 6*x + 7
>> poly2sym(a+b)
ans =
6*x^3 + 8*x^2 + 10*x + 12
>> y1+y2
ans =
6*x^3 + 8*x^2 + 10*x + 12
多项式乘法,conv
>> c=conv(a,b)
c =
8 22 43 72 70 58 35
>> y=poly2sym(c)
y =
8*x^6 + 22*x^5 + 43*x^4 + 72*x^3 + 70*x^2 + 58*x + 35
多项式除法,deconv
5、关系逻辑运算
& 与操作
| 或操作
~ 非操作
xor 异或操作
数组运算时,运算符需加上点号,即:“.-”、“.+”
>> a=[1 2 3;4 5 6;7 8 9]
a =
1 2 3
4 5 6
7 8 9
>> b=[1 1 1;2 2 2;3 3 3]
b =
1 1 1
2 2 2
3 3 3
>> a./b
ans =
1.0000 2.0000 3.0000
2.0000 2.5000 3.0000
2.3333 2.6667 3.0000
>> a/b
Warning: Matrix is singular to working precision.
ans =
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
>> a.*b
ans =
1 2 3
8 10 12
21 24 27
>> a*b
ans =
14 14 14
32 32 32
50 50 50
数组与数的运算:常数写在前,点号运算符号,数组写在后
>> 3+b
ans =
4 4 4
5 5 5
6 6 6
>> 3.+b
ans =
4 4 4
5 5 5
6 6 6
>> b.+3
??? b.+3
|
Error: Unexpected MATLAB operator.
>> a^3
ans =
468 576 684
1062 1305 1548
1656 2034 2412
>> a.^3
ans =
1 8 27
64 125 216
343 512 729
指数运算,exp
>> exp(a)
ans =
1.0e+003 *
0.0027 0.0074 0.0201
0.0546 0.1484 0.4034
1.0966 2.9810 8.1031
>> log(a)
ans =
0 0.6931 1.0986
1.3863 1.6094 1.7918
1.9459 2.0794 2.1972
>> sqrt(a)
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
数组寻址
>> a=rand(1,10)
a =
0.0782 0.4427 0.1067 0.9619 0.0046 0.7749 0.8173 0.8687 0.0844 0.3998
>> a(4)
ans =
0.9619
>> a(2:6)
ans =
0.4427 0.1067 0.9619 0.0046 0.7749
数组排序
>> sort(a)
ans =
0.0046 0.0782 0.0844 0.1067 0.3998 0.4427 0.7749 0.8173 0.8687 0.9619
二、字符串
a、用单引号引用
>> str='this is a string'
str =
this is a string
>> whos
Name Size Bytes Class Attributes
a 2x3 48 double
ans 1x10 80 double
b 3x3 72 double
d 3x3 72 double
e 3x3 72 double
l 3x3 72 double
p 3x3 72 double
q 3x3 72 double
r 3x3 72 double
str 1x16 32 char
u 3x3 72 double
v 1x4 32 double
>> whos str
Name Size Bytes Class Attributes
str 1x16 32 char
>> string1='this'
string1 =
this
>> string2='is'
string2 =
is
>> str=[string1,string2]
str =
thisis
>> str(3)
ans =
i
>> str(2:4)
ans =
his
b、字符串变为ASCII码。用abs函数
>> str='this is bed'
str =
this is bed
>> a=abs(str)
a =
116 104 105 115 32 105 115 32 98 101 100
c、ASCII变字符串,char函数
>> char(a)
ans =
this is bed
d、字符串比较,strcmp
>> str1='this'
str1 =
this
>> str2='this '
str2 =
this
>> strcmp(str1,str2)
ans =
0
>> str3='this'
str3 =
this
>> strcmp(str1,str3)
ans =
1
字符串相等为1,不等为0
其中的某个字符比较,strncmp
>> strcmp(str1,str3)
ans =
1
>> strncmp(str1,str2,3)
ans =
1
e、字符的运算比较
== 等于 eq
~=不等于ne
<小于lt
>大于gt
<=小于等于le
>=大于等于ge
>> str1='matlab'
str1 =
matlab
>> str2='MATLAB'
str2 =
MATLAB
>> str1<str2
ans =
0 0 0 0 0 0
>> str1>str2
ans =
1 1 1 1 1 1 说明小写字母对应的ASCII码的值比大写字母的大
f、字符串的查找与替换
findstr、strfind、strrep
>> str='this is a book'
str =
this is a book
>> strfind(str,'s') 查找字符‘s',出现在第4和7的位置
ans =
4 7
g、字符串与数值的转换
hex2dec 将16进制字符转换成10进制整数
dec2hex 将10进制整数转换成16进制字符串
bin2dec 将2进制字符转换成10进制整数
dec2bin 将10进制整数转换成2进制字符串
base2dec 将B底字符串转换成10进制整数
hex2num 将16进制
upper
lower
fprintf
sprintf
sscanf
char
num2str
int2str
数组与字符的转换
str=mat2str(mat)将二维矩阵mat转换成一个字符串str
h、多项式
1、多项式生成
直接输入系数法
>> p=[2 3 4 5 6 0 7] 定义系数项
p =
2 3 4 5 6 0 7
>> poly2sym(p)
ans =
2*x^6 + 3*x^5 + 4*x^4 + 5*x^3 + 6*x^2 + 7
特征多项式输入法
>> a=[1 2 3 4;5 6 7 8;8 7 6 5;4 3 2 1]
a =
1 2 3 4
5 6 7 8
8 7 6 5
4 3 2 1
>> poly(a)
ans =
1.0000 -14.0000 -72.0000 0.0000 0.0000
>> poly2sym(poly(a))
ans =
x^4 - 14*x^3 - 72*x^2 + (3460367834469499*x)/39614081257132168796771975168 + 2691467142043917/44601490397061246283071436545296723011960832
由多项式的根逆推多项式
>> root=[-4 -2+2i -2-2i 5]
root =
-4.0000 -2.0000 + 2.0000i -2.0000 - 2.0000i 5.0000
>> p=poly(root)
p =
1 3 -16 -88 -160
>> disp(poly2sym(p))
x^4 + 3*x^3 - 16*x^2 - 88*x - 160
2、多项式求值,polyval数组的计算求值、polyvalm矩阵计算求值
>> p=[1 -20 -16 480 98]
p =
1 -20 -16 480 98
>> x=4
x =
4
>> polyval(p,x) 求x=4时,多项式的值
ans =
738
3、求多项式的根
>> roots(p)
ans =
19.5494
5.3052
-4.6514
-0.2031
4、多项式四则运算
加减法
>> a=[2 3 4 5]
a =
2 3 4 5
>> b=[4 5 6 7]
b =
4 5 6 7
>> a+b
ans =
6 8 10 12
>> y1=poly2sym(a)
y1 =
2*x^3 + 3*x^2 + 4*x + 5
>> y2=poly2sym(b)
y2 =
4*x^3 + 5*x^2 + 6*x + 7
>> poly2sym(a+b)
ans =
6*x^3 + 8*x^2 + 10*x + 12
>> y1+y2
ans =
6*x^3 + 8*x^2 + 10*x + 12
多项式乘法,conv
>> c=conv(a,b)
c =
8 22 43 72 70 58 35
>> y=poly2sym(c)
y =
8*x^6 + 22*x^5 + 43*x^4 + 72*x^3 + 70*x^2 + 58*x + 35
多项式除法,deconv
5、关系逻辑运算
& 与操作
| 或操作
~ 非操作
xor 异或操作
相关文章推荐
- 学习matlab 1
- matlab基础学习——数值计算
- Matlab 曲线拟合工具箱 cftool
- 基于MATLAB GUI界面的MCU串口实时绘图设计
- crossvalind Matlab 交叉验证
- matlab三维绘图
- Matlab中出现:尝试将SCRIPT SVD作为函数执行:C:\User\lc\...问题的解决方法
- Matlab 生成mexwin64文件时遇到的问题
- Matlab中 "csrot 找不到指定的模块"的解决方案
- Matlab 中"csrot 找不到指定的模块"的解决方案
- FIT文件格式解析及MATLAB读取程序
- Matlab 启动时,加载失败的问题 Encountered unexpected end-of-file
- matlab基础学习——向量与矩阵
- matlab基础学习——标号、符号、运算符号
- Matlab中如何将(自定义)函数作为参数传递给另一个函数
- Windows10 + Matlab2013 mex C++ 调用gsl
- MATLAB脚本图片格式eps转pdf
- MATLAB:读取文本数据并排序
- MATLAB中对视频进行提取图像
- Matlab启动时大量java错误的处理方法