matlab 自定义函数问题
functionwhatiwant=findsth(a,x)[n,m]=size(a);b=zeros(1,n*m);k=1;fori=1:1:n;forj=1:1:m;...
function whatiwant=findsth(a,x)
[n,m]=size(a);
b=zeros(1,n*m);
k=1;
for i=1:1:n;
for j=1:1:m;
b(1,k)=a(i,j);
k=k+1;
end;
end;
c=sort(b);
whatiwant=(c(1,n*m/2)+c(1,n*m/2+1))/2;
我想的是这样的,自定义一个函数,传a矩阵进去,然后求出中位数
可是提示[n,m]=size(a);这一步有错,请问该怎么改?
或者,这一段有什么更优化的办法?
其实不只是中位数,也许是50%的,也许是30%,那个x就是干这个事的。
怎么传矩阵呢? 展开
[n,m]=size(a);
b=zeros(1,n*m);
k=1;
for i=1:1:n;
for j=1:1:m;
b(1,k)=a(i,j);
k=k+1;
end;
end;
c=sort(b);
whatiwant=(c(1,n*m/2)+c(1,n*m/2+1))/2;
我想的是这样的,自定义一个函数,传a矩阵进去,然后求出中位数
可是提示[n,m]=size(a);这一步有错,请问该怎么改?
或者,这一段有什么更优化的办法?
其实不只是中位数,也许是50%的,也许是30%,那个x就是干这个事的。
怎么传矩阵呢? 展开
2个回答
展开全部
matlab自带了中位数的函数median
如果要求2维矩阵,可以先加一个
b=a(:);
>> help median
MEDIAN Median value.
For vectors, MEDIAN(X) is the median value of the elements in X.
For matrices, MEDIAN(X) is a row vector containing the median
value of each column. For N-D arrays, MEDIAN(X) is the median
value of the elements along the first non-singleton dimension
of X.
MEDIAN(X,DIM) takes the median along the dimension DIM of X.
Example: If X = [0 1 2
3 4 5]
then median(X,1) is [1.5 2.5 3.5] and median(X,2) is [1
4]
See also MEAN, STD, MIN, MAX, COV.
如果要求2维矩阵,可以先加一个
b=a(:);
>> help median
MEDIAN Median value.
For vectors, MEDIAN(X) is the median value of the elements in X.
For matrices, MEDIAN(X) is a row vector containing the median
value of each column. For N-D arrays, MEDIAN(X) is the median
value of the elements along the first non-singleton dimension
of X.
MEDIAN(X,DIM) takes the median along the dimension DIM of X.
Example: If X = [0 1 2
3 4 5]
then median(X,1) is [1.5 2.5 3.5] and median(X,2) is [1
4]
See also MEAN, STD, MIN, MAX, COV.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询