>> y=inline('sin(x)/(1+3*(x)+(x)^2)','x')
y =
内联函数:
y(x) = sin(x)/(1+3*(x)+(x)^2)
>> y(pi/4)
ans =
0.1780
function Z = num(x1,x2,x3)
%UNTITLED2 此处显示有关此函数的摘要
% 此处显示详细说明
Z=x1^2+2*x2^2+3*x3^2+2*x1*x2+3*x2*x3+5*x2*x3;
end
>> num(1,2,3)
ans =
88
function num =OPP(m)
%UNTITLED4 此处显示有关此函数的摘要
% 此处显示详细说明
n=num2str(m); %将整数转化为字符型数组
l=length(n); %计算字符型数组的长度
num=0;
for i=1:l
for j=i+1:l
if n(i)>n(j)
num=num+1;
end
end
end
>> OPP(84723536)
ans =
16
>> OPP(52397899)
ans =
4
function find_m(p,m,n)
%UNTITLED5 此处显示有关此函数的摘要
% 此处显示详细说明
maxx=p(1,1);minn=p(1,1);
for i=1:m
for j=1:n
if p(i,j)>=maxx
maxx=p(i,j);
a1=i;b1=j;
end
if p(i,j)<=minn
minn=p(i,j);
a11=i;b11=j;
end
end
end
fprintf('the biggest number is%f,the position is(%d,%d)\n',maxx,a1,b1);
fprintf('the smallest number is%f,the position is(%d,%d)',minn,a11,b11);
end
>> find_m([1,2,3;4,5,6;7,8,9],3,3)
the biggest number is9.000000,the position is(3,3)
the smallest number is1.000000,the position is(1,1)
function mid_num = find_mid(V)
%UNTITLED 此处显示有关此函数的摘要
% 此处显示详细说明
V=sort(V);
l=length(V);
if mod(l,2)==0
l1=int8(l/2);l2=int8(l/2+1);
mid_num=(V(l1)+V(l2))/2;
else
l=round(l/2);
mid_num=V(l);
end
end
>> find_mid([2,3,4,1,5])
ans =
3
老老实实写了个函数,发现matlab有直接求中位数的函数:
function mid_num = find_mid2(V)
%UNTITLED2 此处显示有关此函数的摘要
% 此处显示详细说明
mid_num=median(V);
end
>> find_mid2([2,3,4,1,5])
ans =
3
function A = build(n)
%UNTITLED3 此处显示有关此函数的摘要
% 此处显示详细说明
x=1:n^2;
A=reshape(x,n,n)';
end
>> build(3)
ans =
1 2 3
4 5 6
7 8 9
function M = addd(x)
%UNTITLED8 此处显示有关此函数的摘要
% 此处显示详细说明
x(3,:)=x(3,:)+x(8,:);
x(4,:)=x(4,:)+x(9,:);
x(6,:)=x(6,:)+x(1,:);
x(7,:)=x(7,:)+x(2,:);
x(:,3)=x(:,3)+x(:,8);
x(:,4)=x(:,4)+x(:,9);
x(:,6)=x(:,6)+x(:,1);
x(:,7)=x(:,7)+x(:,2);
M=x(3:7,3:7);
end
>> addd([0 0 0 0 1 0 0 0 0;0 0 0 6 0 2 0 0 0;0 0 11 0 7 0 3 0 0;0 16 0 12 0 8 0 4 0;21 0 17 0 13 0 9 0 5;0 22 0 18 0 14 0 10 0;0 0 23 0 19 0 15 0 0;0 0 0 24 0 20 0 0 0;0 0 0 0 25 0 0 0 0])
ans =
11 24 7 20 3
4 12 25 8 16
17 5 13 21 9
10 18 1 14 22
23 6 19 2 15
for i=2:1100
flag=1;
for j=2:i-1
if mod(i,j)==0
flag=0;
break
end
end
if flag
fprintf('%d',i)
end
end
%结果太长
function R= relative(X,Y)
%UNTITLED7 此处显示有关此函数的摘要
% 此处显示详细说明
x=mean(X);y=mean(Y);
l=length(X);
sxx=0;syy=0;sxy=0;
for i=1:l
sxx=sxx+(X(i)-x)^2;
syy=syy+(Y(i)-y)^2;
sxy=sxy+(X(i)-x)*(Y(i)-y);
end
R=sxy/(sxx*syy)^(1/2);
end
>> relative([2.5,7.8,6.5],[4,5,6])
ans =
0.7240