我有一个带有一个相当明显瓶颈的 MATLAB 例程。我已经对函数进行了概要分析,结果在函数中使用了 2/3 的计算时间levels
:
该函数levels
接受一个浮点矩阵并将每一列拆分为nLevels
桶,返回一个与输入大小相同的矩阵,每个条目都替换为其所属桶的编号。
为此,我使用quantile
函数来获取存储桶限制,并使用循环将条目分配给存储桶。这是我的实现:
function [Y q] = levels(X,nLevels)
% "Assign each of the elements of X to an integer-valued level"
p = linspace(0, 1.0, nLevels+1);
q = quantile(X,p);
if isvector(q)
q=transpose(q);
end
Y = zeros(size(X));
for i = 1:nLevels
% "The variables g and l indicate the entries that are respectively greater than
% or less than the relevant bucket limits. The line Y(g & l) = i is assigning the
% value i to any element that falls in this bucket."
if i ~= nLevels % "The default; doesnt include upper bound"
g = bsxfun(@ge,X,q(i,:));
l = bsxfun(@lt,X,q(i+1,:));
else % "For the final level we include the upper bound"
g = bsxfun(@ge,X,q(i,:));
l = bsxfun(@le,X,q(i+1,:));
end
Y(g & l) = i;
end
我能做些什么来加快速度吗?代码可以向量化吗?