clc ;
clear all;
close all;
A = [ 1 ,2 ,3,4;2,3,4,5;3,4,5,6]
A = 1 2 3 4 2 3 4 5 3 4 5 6
B = [sin(pi/2),4*8]
B = 1 32
C = [3+2i,2+6i,4-3i]
C = 3.0000 + 2.0000i 2.0000 + 6.0000i 4.0000 - 3.0000i
a=1:9;
b = reshape(a,3,3)
c = reshape(a,3,3)‘ % c为b的转置.
b = 1 4 7 2 5 8 3 6 9 c = 1 2 3 4 5 6 7 8 9
A = rand(4,4)
B = diag(A) % 这里B并不是对角矩阵
C =diag(B) % c才是A中元素的对角部分.
A = 0.4299 0.3968 0.2160 0.6713 0.8878 0.8085 0.7904 0.4386 0.3912 0.7551 0.9493 0.8335 0.7691 0.3774 0.3276 0.7689 B = 0.4299 0.8085 0.9493 0.7689 C = 0.4299 0 0 0 0 0.8085 0 0 0 0 0.9493 0 0 0 0 0.7689
A=[1,2;3,4]; B=[5,6;7,8]; x1=A+B x2=A-B
x1 = 6 8 10 12 x2 = -4 -4 -4 -4
A=[2 5 7 ;1 3 6 ;3 2 5 ];
B=[1 3 5 ; 2 4 6 ;5 6 7 ];
c1=A*B %矩阵相乘必须有A为[m,n],B为[n,p]矩阵,即为A的列数需要和B的行数相等.
c2=B*A
c1 = 47 68 89 37 51 65 32 47 62 c2 = 20 24 50 26 34 68 37 57 106
%矩阵的除法分为左除和右除,
% A\B=inv(A)*B
% B/A = B*inv(A)
a=[11 5 3; 78 5 21; 7 15 9 ];
b=[20 30 40;30 40 50 ; 40 50 60];
c1=a\b
c2=b/a
d1=inv(a)*b
d2=b*inv(a)
%比较c1,d1和c2,d2,既可以看到是否相等.
c1 = 0.7692 1.5385 2.3077 3.6923 5.7179 7.7436 -2.3077 -5.1709 -8.0342 c2 = -9.9786 1.2222 4.9188 -11.3034 1.4444 5.9530 -12.6282 1.6667 6.9872 d1 = 0.7692 1.5385 2.3077 3.6923 5.7179 7.7436 -2.3077 -5.1709 -8.0342 d2 = -9.9786 1.2222 4.9188 -11.3034 1.4444 5.9530 -12.6282 1.6667 6.9872
%乘方的要求为:A^x,其中A为方阵,x为标量.
A=[3 0 7; 9 12 8; 1 5 3];
result = A^3 %A为方阵,x为标量.
result = 405 630 518 2268 3123 3022 884 1180 1125
% 点运算分为 .* 、 ./ 、.\、 .^
%【两个矩阵进行点运算】是指对应元素进行运算。
%A.*B 指的是对应元素相乘.
%A./B 表示对应元素相除
%A.^B表示对应元素做乘方运算.
%【要求】两矩阵具有相同的维数。
A = [1 3 5;2 4 6; 3 6 9]
B= [9 10 7 ; 6 9 4; 2 5 8]
C=A.*B % A.*B 指的是对应元素相乘.
% A./B == B.\A
C1=A./B %A./B 表示对应元素相除
C2=B.\A
x=[2 2 2 ]
y=[1 2 3 ]
z=x.^y %x.^y表示对应元素做乘方运算.
A = 1 3 5 2 4 6 3 6 9 B = 9 10 7 6 9 4 2 5 8 C = 9 30 35 12 36 24 6 30 72 C1 = 0.1111 0.3000 0.7143 0.3333 0.4444 1.5000 1.5000 1.2000 1.1250 C2 = 0.1111 0.3000 0.7143 0.3333 0.4444 1.5000 1.5000 1.2000 1.1250 x = 2 2 2 y = 1 2 3 z = 2 4 8
x=1:2:9 y=x.^2.*cos(x)
x = 1 3 5 7 9 y = 0.5403 -8.9099 7.0916 36.9412 -73.8016
% 关系运算是以矩阵内元素对元素的方式做运算的.
a=[1 2 ; 3 4]
b=[1 3;2 4]
c1 = a>b %矩阵对应元素比较
c2 = a==b
c3 = a~=b
c4 = a<b
c5 = a<=b
c6 = a>=b
d= a(c6) %取出a中非零的元素.
a = 1 2 3 4 b = 1 3 2 4 c1 = 0 0 1 0 c2 = 1 0 0 1 c3 = 0 1 1 0 c4 = 0 1 0 0 c5 = 1 1 0 1 c6 = 1 0 1 1 d = 1 3 4
% 逻辑运算有与(&),或(|),非(~)
% and(a,b) or(a,b) not(a) xor(a,b)
a = [17 26 39 3 -32]
b = [23 168 58 -3 25]
c1 = a>10
c2 = b<10
d1 = a>10 & b<10
% 追个比较: a>10的结果是 [1 ,1 ,1,0,0]
% b<10的结果是[0 ,0 , 0,1,0]
%上面两个结果相与操作是 [0 , 0, 0,0,0]
% 关系与逻辑运算函数
a = 17 26 39 3 -32 b = 23 168 58 -3 25 c1 = 1 1 1 0 0 c2 = 0 0 0 1 0 d1 = 0 0 0 0 0
resultALL = all([10 20 30 40 ]) %均非零,结果为1.
resultALL = 1
resultANY = any([1 2 3 4 5 0]) %有元素非零,结果为1.
resultANY = 1
resultEXIST = exist(‘plot‘)
resultEXIST = 5
dataforFIND = [2 1 0; 6 0 3 ;5 2 4 ]
resultFIND1 = find(dataforFIND)
resultFIND2 = find(dataforFIND‘)
% 从结果来看,find的顺序是
order = [ 1 4 7;2 5 8 ; 3 6 9]
dataforFIND = 2 1 0 6 0 3 5 2 4 resultFIND1 = 1 2 3 4 6 8 9 resultFIND2 = 1 2 4 6 7 8 9 order = 1 4 7 2 5 8 3 6 9
resultISEMPTY = isempty([1 2 3 ; 4 5 6 ; 7 8 9 ]) %矩阵显然非空,则为0
resultISEMPTY = 0
x = [0 -1 -2 4 -4 9] absOFx = abs(x)
x = 0 -1 -2 4 -4 9 absOFx = 0 1 2 4 4 9
a=[3+4i,5+5i;5+12i 3]
angleOFa=angle(a)*(180/pi) %将弧度转换为度数.
a = 3.0000 + 4.0000i 5.0000 + 5.0000i 5.0000 +12.0000i 3.0000 angleOFa = 53.1301 45.0000 67.3801 0
a=[7-8i 10+i;3 7+2i;12-6i 3] realOFa = real(a) imagOFa = imag(a) realANDimag = real(a) + imag(a)*i
a = 7.0000 - 8.0000i 10.0000 + 1.0000i 3.0000 7.0000 + 2.0000i 12.0000 - 6.0000i 3.0000 realOFa = 7 10 3 7 12 3 imagOFa = -8 1 0 2 -6 0 realANDimag = 7.0000 - 8.0000i 10.0000 + 1.0000i 3.0000 7.0000 + 2.0000i 12.0000 - 6.0000i 3.0000
a=[7-8i 10+i;3 7+2i;12-6i 3] conjOFa = conj(a) realANDimag = real(a) - imag(a)*j
a = 7.0000 - 8.0000i 10.0000 + 1.0000i 3.0000 7.0000 + 2.0000i 12.0000 - 6.0000i 3.0000 conjOFa = 7.0000 + 8.0000i 10.0000 - 1.0000i 3.0000 7.0000 - 2.0000i 12.0000 + 6.0000i 3.0000 realANDimag = 7.0000 + 8.0000i 10.0000 - 1.0000i 3.0000 7.0000 - 2.0000i 12.0000 + 6.0000i 3.0000
a = [1 -3 3 ;2 -1 6] resultOFa = exp(a)
a = 1 -3 3 2 -1 6 resultOFa = 2.7183 0.0498 20.0855 7.3891 0.3679 403.4288
a = [0 2 0 ;-2 0 3 ; 0 2 -1] resultOFexpm = expm(a) resultOFexp = exp(1)^a
a = 0 2 0 -2 0 3 0 2 -1 resultOFexpm = -1.1709 2.2727 2.4144 -2.2727 1.2435 2.2018 -1.6096 1.4679 2.1191 resultOFexp = -1.1709 2.2727 + 0.0000i 2.4144 + 0.0000i -2.2727 - 0.0000i 1.2435 + 0.0000i 2.2018 + 0.0000i -1.6096 + 0.0000i 1.4679 + 0.0000i 2.1191 + 0.0000i
a=[2 5 8 9] resultOFsqrt = sqrt(a)
a = 2 5 8 9 resultOFsqrt = 1.4142 2.2361 2.8284 3.0000
a = [12 3 7;-1 4 -2; 21 17 -5] resultOFlog = log(a) resultOFlog2 = log2(a) resultOFlog10 = log10(a)
a = 12 3 7 -1 4 -2 21 17 -5 resultOFlog = 2.4849 1.0986 1.9459 0 + 3.1416i 1.3863 0.6931 + 3.1416i 3.0445 2.8332 1.6094 + 3.1416i resultOFlog2 = 3.5850 1.5850 2.8074 0 + 4.5324i 2.0000 1.0000 + 4.5324i 4.3923 4.0875 2.3219 + 4.5324i resultOFlog10 = 1.0792 0.4771 0.8451 0 + 1.3644i 0.6021 0.3010 + 1.3644i 1.3222 1.2304 0.6990 + 1.3644i
a = [0.5 0.1 -0.2; -0.8 0.3 1.6]
% round(a) | ←!→| 向最接近的整数舍入.
resultOFround = round(a)
% floor(a) |←!| 向负无穷方向舍入.
resultOFfloor = floor(a)
% ceil(a) |!→| 向正无穷方向舍入.
resultOFceil = ceil(a)
% fix(a) !→|0|←! 向零方向舍入.
resultOFfix = fix(a)
a = 0.5000 0.1000 -0.2000 -0.8000 0.3000 1.6000 resultOFround = 1 0 0 -1 0 2 resultOFfloor = 0 0 -1 -1 0 1 resultOFceil = 1 1 0 0 1 2 resultOFfix = 0 0 0 0 0 1
% mod(x,y) 表示x对y取模, mod(x,y) = x-y.*floor(x./y)
% rem(x,y) 表示x对y求余数,rem(x,y) = x-y.*fix(x./y)
a = [5 6 7;-3 -2 -1;18 -16 9]
b = [2 3 -2; 1 5 6;-3 -1 7]
ResultOfAMod3 = mod(a,3)
ResultOfAModB = mod(a,b)
ResultOfARemB = rem(a,b)
a = 5 6 7 -3 -2 -1 18 -16 9 b = 2 3 -2 1 5 6 -3 -1 7 ResultOfAMod3 = 2 0 1 0 1 2 0 2 0 ResultOfAModB = 1 0 -1 0 3 5 0 0 2 ResultOfARemB = 1 0 1 0 -2 -1 0 0 2
m = 4
n = 5
x = rand(n) %生成n*n随机矩阵,其元素在0-1内.
x = rand(m,n) %生成m*n随机矩阵.
x = rand([m,n]) %生成m*n随机矩阵
% x = rand(m,n,p...) %生成m*n*p*...随机矩阵
% x = rand([m n p ... ]) %生成m*n*p*...随机矩阵
x = rand(size(A)) %生成与矩阵A相同大小的随机矩阵.
resultofRAND1 = rand %无变量输入时只产生一个随机数
resultofRAND2 = rand(‘state‘) %产生包括均衡发生器当前状态的35个随机数
% resultofRAND3 = rand(‘state‘,s) %状态重置为s
% resultofRAND4 = rand(‘state‘,0) %重置发生器到初始状态
% resultofRAND5 = rand(‘state‘,j) %对整数j重置发生器到第j个状态
% resultofRAND6 = rand(‘state‘,sum(100*clock)) % 每次重置到不同状态
m = 4 n = 5 x = 0.1673 0.5880 0.8256 0.1117 0.4950 0.8620 0.1548 0.7900 0.1363 0.1476 0.9899 0.1999 0.3185 0.6787 0.0550 0.5144 0.4070 0.5341 0.4952 0.8507 0.8843 0.7487 0.0900 0.1897 0.5606 x = 0.9296 0.8790 0.6126 0.8013 0.5747 0.6967 0.9889 0.9900 0.2278 0.8452 0.5828 0.0005 0.5277 0.4981 0.7386 0.8154 0.8654 0.4795 0.9009 0.5860 x = 0.2467 0.6609 0.7690 0.0170 0.8449 0.6664 0.7298 0.5814 0.1209 0.2094 0.0835 0.8908 0.9283 0.8627 0.5523 0.6260 0.9823 0.5801 0.4843 0.6299 x = 0.0320 0.0495 0.1231 0.6147 0.4896 0.2055 0.3624 0.1925 0.1465 resultofRAND1 = 0.1891 resultofRAND2 = 0.8301 0.6705 0.0845 0.0686 0.0371 0.3854 0.1653 0.3752 0.7297 0.4534 0.8596 0.5685 0.9848 0.3742 0.3715 0.9499 0.9774 0.7428 0.4958 0.4157 0.0777 0.3299 0.9429 0.0906 0.3091 0.5518 0.0350 0.0018 0.9854 0.8229 0.4586 0.9710 0 0 0.0000
m = 4
n = 5
x = randn(n) %生成n*n随机矩阵,其元素在0-1内.
x = randn(m,n) %生成m*n随机矩阵.
x = randn([m,n]) %生成m*n随机矩阵
% x = randn(m,n,p...) %生成m*n*p*...随机矩阵
% x = randn([m n p ... ]) %生成m*n*p*...随机矩阵
x = randn(size(A)) %生成与矩阵A相同大小的随机矩阵.
resultofRAND1 = randn %无变量输入时只产生一个随机数
resultofRAND2 = randn(‘state‘) %产生包括均衡发生器当前状态的35个随机数
% resultofRAND3 = randn(‘state‘,s) %状态重置为s
% resultofRAND4 = randn(‘state‘,0) %重置发生器到初始状态
% resultofRAND5 = randn(‘state‘,j) %对整数j重置发生器到第j个状态
% resultofRAND6 = randn(‘state‘,sum(100*clock)) % 每次重置到不同状态
m = 4 n = 5 x = -1.7254 -0.0022 0.9608 0.3763 -0.5100 0.2882 0.0931 1.7382 -0.2270 -1.3216 -1.5942 -0.3782 -0.4302 -1.1489 -0.6361 0.1102 -1.4827 -1.6273 2.0243 0.3179 0.7871 -0.0438 0.1663 -2.3595 0.1380 x = -0.7107 -0.4256 1.0635 -0.3712 -0.5568 0.7770 1.0486 1.1569 -0.7578 -0.8951 0.6224 0.6607 0.0530 -0.5640 -0.4093 0.6474 2.5088 -1.2884 0.5551 -0.1609 x = 0.4093 1.3244 -1.3853 -0.8927 -0.2269 -0.9526 -0.2132 0.3105 1.9085 -0.1625 0.3173 -0.1345 -0.2495 0.1222 0.6901 0.0780 -1.1714 0.5037 1.0470 0.5558 x = -1.1203 -1.4158 -0.3680 -1.5327 0.0596 -1.3610 -1.0979 -0.4113 0.7796 resultofRAND1 = 0.4394 resultofRAND2 = 362436069 521288629
% x = normrnd (MU,SIGMA) 返回均值为MU,标准差为SIGMA的正态分布随机数据,x可以是矩阵或者向量.
% x = normrnd (MU,SIGMA,m) m指定随机数的个数.
% x = normrnd (MU,SIGMA,m,n) m,n表示行数和列数.
% 产生10个均值为2,方差5的随机数.
x = normrnd (2,sqrt(5),1,10)
x = 2 + sqrt(5)*randn(1,10)
x = 1.7996 4.2834 0.0457 2.9273 2.7791 2.7810 0.3694 2.7308 0.8487 -0.0045 x = -0.6906 4.3206 0.1084 1.6134 -0.7026 1.3356 -5.2271 -0.4305 -1.1896 -0.2684
f = ‘exp(x)‘
f = ‘a*x^2+bx+c=0‘
f = ‘D2y - 2Dy - 3y =0‘
f = exp(x) f = a*x^2+bx+c=0 f = D2y - 2Dy - 3y =0
A = sym(‘[a b c ; e f g ]‘)
f = sym(‘ax +b =0‘)
A = [ a, b, c] [ e, f, g] f = ax + b = 0
syms y u;
p = exp(-y/u)
p = 1/exp(y/u)
f = sym (‘a*x^2/(b-x)‘)
[n,d] = numden(f)
f = (a*x^2)/(b - x) n = a*x^2 d = b - x
B = sym(‘x+1‘)
C = sym(‘x^2-1‘)
D = B + C
B = x + 1 C = x^2 - 1 D = x^2 + x
% compose(f,g)
% compose(f,g,z)
% compose(f,g,x,z)
% compose(f,g,x,y,z)
syms x,y;
f = 1/x^3
g = tan(y)
compose(f,g)
% 任意建立矩阵A,然后找出在[10 ,20 ]区间的元素的位置.
a=[10 11 8 9 20 44 40]
ElementBetween10and20 = a(a>=10 & a<=20)
f = 1/x^3 g = tan(y) ans = 1/tan(y)^3 a = 10 11 8 9 20 44 40 ElementBetween10and20 = 10 11 20
附:MATLAB基础知识简介QP
matlab入门基础知识精心整理比较完整
MATLAB优化函数
原文:http://www.cnblogs.com/fclbky/p/5159503.html