书籍:Matlab实用教程
工具:Matlab2021a
电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 windows10 专业版
第2章 MATLAB数值计算
2.2 矩阵和数组
2.2.3 字符串
cpp
>> str1='Hello'
str1 =
'Hello'
>> str2='I''m a chinese.'
str2 =
'I'm a chinese.'
>> whos
Name Size Bytes Class Attributes
str1 1x5 10 char
str2 1x14 28 char
>> length(str1)
ans =
5
>> x1=double(str1)
x1 =
72 101 108 108 111
>> x2=x1+1
x2 =
73 102 109 109 112
>> char(x1)
ans =
'Hello'
>> char(x2)
ans =
'Ifmmp'
>> class(str1)
ans =
'char'
>> class(x1)
ans =
'double'
>> ischar(str1)
ans =
logical
1
>> str3=[str1,str2]
str3 =
'HelloI'm a chinese.'
>> str6=str2mat(str1,str2,str3)
str6 =
3×19 char 数组
'Hello '
'I'm a chinese. '
'HelloI'm a chinese.'
>> str7=strvcat(str1,str2)
str7 =
2×14 char 数组
'Hello '
'I'm a chinese.'
>> str8='a=2*5'
str8 =
'a=2*5'
>> eval(str8)
a =
10
>> disp(str1)
Hello
>>
2.2.4 矩阵和数组运算
cpp
>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> det(A)
ans =
-9.5162e-16
>> rank(A)
ans =
2
>> inv(A)
警告: 矩阵接近奇异值,或者缩放错误。结果可能不准确。RCOND = 2.202823e-18。
ans =
1.0e+16 *
0.3153 -0.6305 0.3153
-0.6305 1.2610 -0.6305
0.3153 -0.6305 0.3153
>> [v,d]=eig(A)
v =
-0.2320 -0.7858 0.4082
-0.5253 -0.0868 -0.8165
-0.8187 0.6123 0.4082
d =
16.1168 0 0
0 -1.1168 0
0 0 -0.0000
>> diag(A)
ans =
1
5
9
>> [l,u]=lu(A)
l =
0.1429 1.0000 0
0.5714 0.5000 1.0000
1.0000 0 0
u =
7.0000 8.0000 9.0000
0 0.8571 1.7143
0 0 -0.0000
>> [q,r]=qr(A)
q =
-0.1231 0.9045 0.4082
-0.4924 0.3015 -0.8165
-0.8616 -0.3015 0.4082
r =
-8.1240 -9.6011 -11.0782
0 0.9045 1.8091
0 0 -0.0000
>> [u,s,v]=svd(A)
u =
-0.2148 0.8872 0.4082
-0.5206 0.2496 -0.8165
-0.8263 -0.3879 0.4082
s =
16.8481 0 0
0 1.0684 0
0 0 0.0000
v =
-0.4797 -0.7767 -0.4082
-0.5724 -0.0757 0.8165
-0.6651 0.6253 -0.4082
>> x1=[1 2;3 4;5 6]
x1 =
1 2
3 4
5 6
>> x2=eye(3,2)
x2 =
1 0
0 1
0 0
>> x1+x2
ans =
2 2
3 5
5 6
>> x1.*x2
ans =
1 0
0 4
0 0
>> x1*x2
错误使用 *
用于矩阵乘法的维度不正确。请检查并确保第一个矩阵中的列数与第二个矩阵中的行数匹配。要执行按元素相乘,请使用 '.*'。
相关文档
>> x3=eye(2,3)
x3 =
1 0 0
0 1 0
>> x1*x3
ans =
1 2 0
3 4 0
5 6 0
>> A=[2 -1 3;3 1 -5;4 -1 1]
A =
2 -1 3
3 1 -5
4 -1 1
>> B=[5 5 9]
B =
5 5 9
>> B=[5;5;9]
B =
5
5
9
>> X=A\B
X =
2.0000
-1.0000
0.0000
>> x1=[1 2;3 4]
x1 =
1 2
3 4
>> x2=eye(2)
x2 =
1 0
0 1
>> x1/x2
ans =
1 2
3 4
>> inv(x1)
ans =
-2.0000 1.0000
1.5000 -0.5000
>> x1\x2
ans =
-2.0000 1.0000
1.5000 -0.5000
>> x1./x2
ans =
1 Inf
Inf 4
>> x1.\x2
ans =
1.0000 0
0 0.2500
>> x1^2
ans =
7 10
15 22
>> x1^-1
ans =
-2.0000 1.0000
1.5000 -0.5000
>> x1^0.2
ans =
0.8397 + 0.3672i 0.2562 - 0.1679i
0.3842 - 0.2519i 1.2239 + 0.1152i
>> 2^x1
ans =
10.4827 14.1519
21.2278 31.7106
>> 2.^x1
ans =
2 4
8 16
>> x1.^x2
ans =
1 1
1 4
>> x3=x1+x2*i
x3 =
1.0000 + 1.0000i 2.0000 + 0.0000i
3.0000 + 0.0000i 4.0000 + 1.0000i
>> x3'
ans =
1.0000 - 1.0000i 3.0000 + 0.0000i
2.0000 + 0.0000i 4.0000 - 1.0000i
>> x3.'
ans =
1.0000 + 1.0000i 3.0000 + 0.0000i
2.0000 + 0.0000i 4.0000 + 1.0000i
>> t=linspace(0,2*pi,6)
t =
0 1.2566 2.5133 3.7699 5.0265 6.2832
>> y=sin(t)
y =
0 0.9511 0.5878 -0.5878 -0.9511 -0.0000
>> y1=abs(y)
y1 =
0 0.9511 0.5878 0.5878 0.9511 0.0000
>> 1-exp(-t).*y
ans =
1.0000 0.7293 0.9524 1.0136 1.0062 1.0000
>> a=0;b=5;c=10;
>> (a~=0)&&(b<c)
ans =
logical
0
>> (a~=0)||(b<c)
ans =
logical
1
>> t=linspace(0,3*pi);
>> y=sin(t)
y =
列 1 至 26
0 0.0951 0.1893 0.2817 0.3717 0.4582 0.5406 0.6182 0.6901 0.7557 0.8146 0.8660 0.9096 0.9450 0.9718 0.9898 0.9989 0.9989 0.9898 0.9718 0.9450 0.9096 0.8660 0.8146 0.7557 0.6901
列 27 至 52
0.6182 0.5406 0.4582 0.3717 0.2817 0.1893 0.0951 -0.0000 -0.0951 -0.1893 -0.2817 -0.3717 -0.4582 -0.5406 -0.6182 -0.6901 -0.7557 -0.8146 -0.8660 -0.9096 -0.9450 -0.9718 -0.9898 -0.9989 -0.9989 -0.9898
列 53 至 78
-0.9718 -0.9450 -0.9096 -0.8660 -0.8146 -0.7557 -0.6901 -0.6182 -0.5406 -0.4582 -0.3717 -0.2817 -0.1893 -0.0951 0.0000 0.0951 0.1893 0.2817 0.3717 0.4582 0.5406 0.6182 0.6901 0.7557 0.8146 0.8660
列 79 至 100
0.9096 0.9450 0.9718 0.9898 0.9989 0.9989 0.9898 0.9718 0.9450 0.9096 0.8660 0.8146 0.7557 0.6901 0.6182 0.5406 0.4582 0.3717 0.2817 0.1893 0.0951 0.0000
>> t1=(t<pi)|(t>2*pi)
t1 =
1×100 logical 数组
列 1 至 66
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
列 67 至 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>> y1=t1.*y
y1 =
列 1 至 26
0 0.0951 0.1893 0.2817 0.3717 0.4582 0.5406 0.6182 0.6901 0.7557 0.8146 0.8660 0.9096 0.9450 0.9718 0.9898 0.9989 0.9989 0.9898 0.9718 0.9450 0.9096 0.8660 0.8146 0.7557 0.6901
列 27 至 52
0.6182 0.5406 0.4582 0.3717 0.2817 0.1893 0.0951 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
列 53 至 78
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.0000 0.0951 0.1893 0.2817 0.3717 0.4582 0.5406 0.6182 0.6901 0.7557 0.8146 0.8660
列 79 至 100
0.9096 0.9450 0.9718 0.9898 0.9989 0.9989 0.9898 0.9718 0.9450 0.9096 0.8660 0.8146 0.7557 0.6901 0.6182 0.5406 0.4582 0.3717 0.2817 0.1893 0.0951 0.0000
>> a=[1 inf;0 2]
a =
1 Inf
0 2
>> b=[0 1;1 0]
b =
0 1
1 0
>> all(a)
ans =
1×2 logical 数组
0 1
>> any(a)
ans =
1×2 logical 数组
1 1
>> isequal(a,b)
ans =
logical
0
>> isempty(a)
ans =
logical
0
>> isfinite(a)
ans =
2×2 logical 数组
1 0
1 1
>> isinf(a)
ans =
2×2 logical 数组
0 1
0 0
>> isnan(a)
ans =
2×2 logical 数组
0 0
0 0
>> isnumeric(a)
ans =
logical
1
>> isreal(a)
ans =
logical
1
>> isprime(b)
ans =
2×2 logical 数组
0 0
0 0
>> isspace(a)
ans =
2×2 logical 数组
0 0
0 0
>> find(b)
ans =
2
3
>>