英文:
Finding median in MATLAB taking a lot of time
问题
我有一个名为X
的行向量,我想找到它的中位数。我正在使用MATLAB的函数median()
来找到中位数,但它正在成为我的代码瓶颈。
是否有其他语言中用于查找中位数的更快实现?
英文:
I have a row vector X
for which I want to find the median.
I am using MATLAB's function median()
to find the same but it is proving to be the bottleneck in my code.
Is there a faster implementation available for finding median in any other language?
答案1
得分: 2
我成功地通过数个数量级提高了计算速度,但没有采用不同的实现方式。相反,我只是将向量转换为gpuArray
,然后传递给median()
函数。对于较大的向量,这减少了计算时间数个数量级。但对于较短的数组,这会减慢速度,我认为这是因为时间花在了将向量传输到GPU内存上。我发现向量的过渡长度约为450。对于长度小于450的向量,直接将向量传递给median()
函数更好,而对于更长的向量,将其转换为gpuArray
,然后传递给median()
函数更快。
英文:
I was able to improve the speed of calculation by several orders of magnitude but not by a different implementation.
Rather, I just converted the vector into a gpuArray
and then fed into the median()
function. For large vectors this reduced the time of calculation by several orders of magnitude. But slowed things down for shorter arrays, I believe this is because time was being used up in transferring the vector to memory in gpu.
I found the transition length of the vector to be ~450. That is for vectors shorter than 450 directly feeding the vector into the median()
function is better and for longer ones converting to gpuArray
and then feeding into the median()
function is faster.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论