我有一个排序的点数组,比如
x=[1 1 1 2 2 4 4 5 6 ......7 8 8 9 9]
我想要一个包含3个元素的数组,其中包含最少的元素,3个元素包含此数组的最大元素(忽略相同的元素)
以上所需的结果将是
ans=[1 2 4 7 8 9]
答案 0 :(得分:3)
有点不那么优雅,但利用排序输入,速度更快。
i = find(diff(x)~=0);
ans = x([i([1:3 end-1:end]) end]);
答案 1 :(得分:2)
您可以使用UNIQUE函数
执行此操作uniqueValues = unique(x); %# Get the unique values of x
minmaxValues = uniqueValues([1:3 end-2:end]); %# Get the 3 smallest and largest