Home Article Practice matlab 常见代码

matlab 常见代码

2022-11-22 11:23  views:813  source:小键人10203313    

m = rand([3,5])
size(m) %获取矩阵行列
m = rand([3,5]);
size(m);
[row, col] = size(m) %分别赋值给变量
m' %转置
inv(m) %求逆
% V 为特征向量矩阵
% D 为特征值矩阵,对角元素为特征值
[ V, D ] = eig(m)
a = [ 1 2; 3 4 ]
b = a
a*b %两个矩阵相乘(a的列数等于b的行数)
a.*b %对应元素相乘
% 斜杆倒向哪个,哪个取逆
a/b % 等价于 a*inv(b) 对 a=xb 求解
a\b % 等价于 inv(a)*b 对 ax=b 求解
a^5 %a*a*a*a*a
a = [ 1 2; 3 4 ] %逻辑运算
b = [ 2 1; 3 4 ]
a == b % 对每个元素进行逻辑判断,对应元素进行逻辑判断
a > b
a == 1 % 会对 1 进行广播 把 1 广播成 [ 1 1; 1 1 ]
%for循环
for a = 10:20
fprintf('value of a: %d', a);
end
for a = 1.0: -0.1: 0.0
disp(a)
end
%分段函数
x=input("请输入x的值")
if x<0&&x~=-3
y=x^2+x-6;
elseif 0<=x&&x<5&&x~=2&&x~=3 %这里是有三个并列条件用&&隔开.
y=x^2-5*x+6;
else
y=x^2-x-1
end
%矩阵合并,逗号和空格表示横向拼接;分号表示纵向拼接
A = zeros(4,2)
B = ones(4,1)
C = [A B]
C = [A;B]
%新建函数
function n=A(n)
if n==1
n=1;
else
n=n+A(n-1)
end
%调用
a=A(3)



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)