matlab中对一个数组求最大的几个数
我需要处理一个1*10000的矩阵,首先按照每m个数一个周期,求每个周期中最大的数,这样就有10000/m个数。再在这些数中求最大的那n个。这里肯定是可以整除。最后,要返...
我需要处理一个1*10000的矩阵,首先按照每m个数一个周期,求每个周期中最大的数,这样就有10000/m个数。再在这些数中求最大的那n个。这里肯定是可以整除。最后,要返回的值是这n个数的值和他们在原来那个1*10000的矩阵中的位置,并且按照在原矩阵中的先后顺序排好。
我举例如下。a=[3,15,6,21,18,2,18,19,1,4,7,29,21,23,29,23,14,6,9,29,31,],对这个数组,每3个数是一个周期,这就是7个周期,会求出7个数。依次是[15,21,19,29,29,23,31]然后在这7个数中取最大的四个数,那应该是[29,29,23,31]。然后返回这四个数和他们在原矩阵中的位置。
记为:a1=29,n1=12;a2=29,n2=15;a3=23,n3=16;a4=31,n4=21。也可以返回成矩阵的形式[29,29,23,31;12,15,16,21]。
Sort in ascending order.For vectors, SORT(X) sorts the elements of X in ascending order.For matrices, SORT(X) sorts each column of X in ascending order.For N-D arrays, SORT(X) sorts the along the first non-singleton dimension of X. When X is a cell array of strings, SORT(X) sorts the strings in ASCII dictionary order.SORT(X,DIM) sorts along the dimension DIM.[Y,I] = SORT(X) also returns an index matrix I.If X is a vector,then Y = X(I).If X is an m-by-n matrix, then for j = 1:n, Y(:,j) = X(I(:,j),j); end When X is complex, the elements are sorted by ABS(X).Complex matches are further sorted by ANGLE(X).When more than one element has the same value, the order of the elements are preserved in the sorted result and the indexes of equal elements will be ascending in any index matrix.Example: If X = [3,7, 5; 0, 4, 2] then sort(X,1) is [0,4,2;3,7,5] and sort(X,2) is [3,5,7;0,2,4];See also SORTROWS, MIN, MAX, MEAN, MEDIAN.Overloaded methods help cell/sort.m;xregdesign/sort.m;sweepset/sort.m 展开
我举例如下。a=[3,15,6,21,18,2,18,19,1,4,7,29,21,23,29,23,14,6,9,29,31,],对这个数组,每3个数是一个周期,这就是7个周期,会求出7个数。依次是[15,21,19,29,29,23,31]然后在这7个数中取最大的四个数,那应该是[29,29,23,31]。然后返回这四个数和他们在原矩阵中的位置。
记为:a1=29,n1=12;a2=29,n2=15;a3=23,n3=16;a4=31,n4=21。也可以返回成矩阵的形式[29,29,23,31;12,15,16,21]。
Sort in ascending order.For vectors, SORT(X) sorts the elements of X in ascending order.For matrices, SORT(X) sorts each column of X in ascending order.For N-D arrays, SORT(X) sorts the along the first non-singleton dimension of X. When X is a cell array of strings, SORT(X) sorts the strings in ASCII dictionary order.SORT(X,DIM) sorts along the dimension DIM.[Y,I] = SORT(X) also returns an index matrix I.If X is a vector,then Y = X(I).If X is an m-by-n matrix, then for j = 1:n, Y(:,j) = X(I(:,j),j); end When X is complex, the elements are sorted by ABS(X).Complex matches are further sorted by ANGLE(X).When more than one element has the same value, the order of the elements are preserved in the sorted result and the indexes of equal elements will be ascending in any index matrix.Example: If X = [3,7, 5; 0, 4, 2] then sort(X,1) is [0,4,2;3,7,5] and sort(X,2) is [3,5,7;0,2,4];See also SORTROWS, MIN, MAX, MEAN, MEDIAN.Overloaded methods help cell/sort.m;xregdesign/sort.m;sweepset/sort.m 展开
3个回答
展开全部
可以利用sort函数给数列a从小到大排列,找前几个最大的。如下:
[b,i]=sort(a)。b为从小到大的数字,i为对应位置。要找前3个,如下输入:
>>
a=[3,15,6,21,18,2,18,19,1,4,7,29,
21
,23
,29
,23,
14,
6,
9
,29
,31];
>>
[b,i]=sort(a)
b
=
columns
1
through
12
1
2
3
4
6
6
7
9
14
15
18
18
columns
13
through
21
19
21
21
23
23
29
29
29
31
i
=
columns
1
through
12
9
6
1
10
3
18
11
19
17
2
5
7
columns
13
through
21
8
4
13
14
16
12
15
20
21
>>
b(19:21)
ans
=
29
29
31
>>
i(19:21)
ans
=
15
20
21
[b,i]=sort(a)。b为从小到大的数字,i为对应位置。要找前3个,如下输入:
>>
a=[3,15,6,21,18,2,18,19,1,4,7,29,
21
,23
,29
,23,
14,
6,
9
,29
,31];
>>
[b,i]=sort(a)
b
=
columns
1
through
12
1
2
3
4
6
6
7
9
14
15
18
18
columns
13
through
21
19
21
21
23
23
29
29
29
31
i
=
columns
1
through
12
9
6
1
10
3
18
11
19
17
2
5
7
columns
13
through
21
8
4
13
14
16
12
15
20
21
>>
b(19:21)
ans
=
29
29
31
>>
i(19:21)
ans
=
15
20
21
展开全部
如果不用库函数的话,可以用下面的代码:
clc;
clear;
a=[3,15,6,21,18,2,18,19,1,4,7,29,21,23,29,23,14,6,9,29,31];
m=3;
n=4;
%以后不需要改动
j=0;
jlast=0;
for i=1:length(a)
if mod(i,m)==1
j=j+1;
end
if j-jlast==1
b(j,:)=[a(i),i];
else
if b(j,1)<a(i)
b(j,1)=a(i);
b(j,2)=i;
end
end
jlast=j;
end
c=b(1:n,:);
for i=n+1:size(b,1)
minc=c(1,:);
pc=1;
for j=2:n
if c(j,1)<minc(1)
minc=c(j,:);
pc=j;
end
end
if b(i,1)>minc(1)
if pc==1;
c=[c(2:n,:);b(i,:)];
elseif pc==n
c=[c(1:n-1,:);b(i,:)];
else
c=[c(1:pc-1,:);c(pc+1:n,:);b(i,:)];
end
end
end
b',c'
clc;
clear;
a=[3,15,6,21,18,2,18,19,1,4,7,29,21,23,29,23,14,6,9,29,31];
m=3;
n=4;
%以后不需要改动
j=0;
jlast=0;
for i=1:length(a)
if mod(i,m)==1
j=j+1;
end
if j-jlast==1
b(j,:)=[a(i),i];
else
if b(j,1)<a(i)
b(j,1)=a(i);
b(j,2)=i;
end
end
jlast=j;
end
c=b(1:n,:);
for i=n+1:size(b,1)
minc=c(1,:);
pc=1;
for j=2:n
if c(j,1)<minc(1)
minc=c(j,:);
pc=j;
end
end
if b(i,1)>minc(1)
if pc==1;
c=[c(2:n,:);b(i,:)];
elseif pc==n
c=[c(1:n-1,:);b(i,:)];
else
c=[c(1:pc-1,:);c(pc+1:n,:);b(i,:)];
end
end
end
b',c'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改好了。呵呵,和我想的一样,是升序。
%下面是你要输入的数据
a=[3,15,6,21,18,2,18,19,1,4,7,29 21 23 29 23 14 6 9 29 31]%原矩阵
m=3%周期
n=4%你要取的前n个数
%下面一个字也不用改
b=reshape(a,m,[]);
[c i]=max(b);
[d,j]=sort(c);
j=sort(j(end:-1:end-n+1));
k=m*(j-1)+i(j);
c=[a(k);k]%答案
%下面是你要输入的数据
a=[3,15,6,21,18,2,18,19,1,4,7,29 21 23 29 23 14 6 9 29 31]%原矩阵
m=3%周期
n=4%你要取的前n个数
%下面一个字也不用改
b=reshape(a,m,[]);
[c i]=max(b);
[d,j]=sort(c);
j=sort(j(end:-1:end-n+1));
k=m*(j-1)+i(j);
c=[a(k);k]%答案
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询