由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Computation版 - 在matlab里怎么找到一个数组里的最大k个元素
相关主题
菜鸟急问:两个数组相加,复杂度是否可以降到O(1)求教PCA
请问矩阵或数组的索引非整数的问题,比如a(1:3.5:20)请教关于MATLAB的一个小问题
问个MATLAB矩阵问题matlab一问
请问matlab矩阵和向量对应元素相乘该怎么做?[help]should I switch from Matlab to C?
c++为什么比c慢?Matlab里用多维数组会不会影响速度?
[Matlab] 同时读取矩阵中不连续的多个元素[合集] 请教一段matlab程序 (转载)
c++, sort() 为啥显示 结果不对sigmaplot问题(zt)
请问怎么判断一个元素为0和1的矩阵变化平缓求助: fitting y=a+bx+cx^-2+dx^2
相关话题的讨论汇总
话题: element话题: matlab话题: log话题: max话题: use
进入Computation版参与讨论
1 (共1页)
d*****y
发帖数: 140
1
vector的长度为n,k< 的跟快的办法么?
thx!
b********e
发帖数: 58
2
not a matlab expert. assuming it is using quick sort, the complexity is n*
log(n) then.
If you just do a scanning, should be fixed at n*k, which is better.
If you maintain a k element array and first sort the first k elements in the
vector, then scan through the rest. Every time if the coming element is
smaller then the minimum, drop it. otherwise, use log(k) steps to insert the
new element in the array and drop the smallest one. The worst case will be
n*(log(k)), even better.
b********e
发帖数: 58
3
not a matlab expert. assuming it is using quick sort, the complexity is n*
log(n) then.
If you just do a scanning, should be fixed at n*k, which is better.
If you maintain a k element array and first sort the first k elements in the
vector, then scan through the rest. Every time if the coming element is
smaller then the minimum, drop it. otherwise, use log(k) steps to insert the
new element in the array and drop the smallest one. The worst case will be
n*(log(k)), even better.
d*****n
发帖数: 754
4
Use max. Remove the max item, then use max again. Repeat k times.

【在 d*****y 的大作中提到】
: vector的长度为n,k<: 的跟快的办法么?
: thx!

x*****s
发帖数: 39
5
1. For example, if the vector n is a random value vector, you can first use
find() to pre-select almost all the k maxima.
ind=find(vectorn>max-(max-min)*k/n)
if the size of ind is greater than that of k, you are almost done. Otherwise
, you can use max() to pick up a few of the missed.
2. Another fast way is to use mex function importing code written in Fortran
or C.

【在 d*****y 的大作中提到】
: vector的长度为n,k<: 的跟快的办法么?
: thx!

h******g
发帖数: 69
6
It should only take around O(k*log2(n)) to find k max numbers in n numbers.
This is a FAQ for algorithm interview.
i am not sure if there is any MATLAB function which implements this
algorithm, but you can write one by yourself.
1 (共1页)
进入Computation版参与讨论
相关主题
求助: fitting y=a+bx+cx^-2+dx^2c++为什么比c慢?
请问如何在matlab中删除vector中的数据[Matlab] 同时读取矩阵中不连续的多个元素
问个matlab的matrix问题c++, sort() 为啥显示 结果不对
如何快速求一个5000*5000的对称矩阵的逆请问怎么判断一个元素为0和1的矩阵变化平缓
菜鸟急问:两个数组相加,复杂度是否可以降到O(1)求教PCA
请问矩阵或数组的索引非整数的问题,比如a(1:3.5:20)请教关于MATLAB的一个小问题
问个MATLAB矩阵问题matlab一问
请问matlab矩阵和向量对应元素相乘该怎么做?[help]should I switch from Matlab to C?
相关话题的讨论汇总
话题: element话题: matlab话题: log话题: max话题: use