让你的方法奏效:
首先我们修正你原来的方法:
a = exprnd(1,10000, 1);
x = 0:0.02:10;
count = zeros(size(x)); %%// <= Preallocate the count-vector
for i = 1:length(x);
%%// <= removed the line "count = 0"
for j = 1:length(a);
if (a(j) <= x(i))
count(i) = count(i) + 1; %%// <= Changed count to count(i)
end
end
end
这将使您的方法奏效。如果你想为相对较大的向量 a 和 x 计算它,这将非常慢,因为整体复杂度是 O(n^2)。 (假设n==length(x)==length(a)。)
您可以使用不同的方法来加快运行时间:
复杂性的方法O(n*log(n)) 使用sort:
这是一个复杂度算法O(n*log(n)),而不是O(n^2)。
它基于 Matlab 的 sort 稳定和返回的位置。假设x已排序,如果再对[a(:); x(:)]进行排序,x(1)的新位置将是1加上a小于等于x(1)的元素个数。 x(2) 的新位置将是2 加上a 的元素个数小于或等于x(2)。所以a中小于x(i)的元素个数等于x(i)的新位置减去i。
function aSmallerThanxMat = aSmallerThanx(a, x)
%%// Remember dimension of x
dimX = size(x);
%%// Sort x and remember original ordering Ix
[xsorted, Ix] = sort(x(:));
%%// How many as are smaller than sortedX
[~,Iaxsorted] = sort([a(:); xsorted(:)]);
Iaxsortedinv(Iaxsorted) = 1:numel(Iaxsorted);
aSmallerThanSortedx = Iaxsortedinv(numel(a)+1:end)-(1:numel(xsorted));
%%// Get original ordering of x back
aSmallerThanx(Ix) = aSmallerThanSortedx;
%%// Reshape x to original array size
aSmallerThanxMat = reshape(aSmallerThanx, dimX);
这种方法可能有点难以掌握,但对于大型向量,您将获得相当大的加速。
使用sort 和for 的类似方法:
这种方法的概念非常相似,但更传统的使用循环:
首先我们对x 和a 进行排序。然后我们单步执行x(i_x)。如果x(i_x) 大于当前a(i_a),我们增加i_a。如果小于当前的i_a,那么i_a-1就是a中小于等于x(i_x)的元素个数。
function aSmallerThanx = aSmallerThanx(a, x)
asorted = sort(a(:));
[xsorted, Ix] = sort(x(:));
aSmallerThanx = zeros(size(x));
i_a = 1;
for i_x = 1:numel(xsorted)
for i_a = i_a:numel(asorted)+1
if i_a>numel(asorted) || xsorted(i_x)<asorted(i_a)
aSmallerThanx(Ix(i_x)) = i_a-1;
break
end
end
end
使用histc:
这个更好:它在 x 的值之间创建 bin,计算落入每个 bin 的 a 的值,然后从左边开始对它们求和。
function result = aSmallerThanx(a, x)
[xsorted, Ix] = sort(x(:));
bincounts = histc(a, [-Inf; xsorted]);
result(Ix) = cumsum(bincounts(1:end-1));
比较:
这是您的方法的运行时比较,Ander Biguri 的 for+sum 循环方法,mehmet 的 bsxfun 方法,这两种方法使用 @987654366 @ 和 histc 方法:
对于长度为 16384 的向量,histc 方法比原始方法快 2300 倍。