在MATLAB中更快地替换find()

时间:2012-06-19 05:48:32

标签: performance matlab optimization

我正在尝试编写(高效的)MATLAB代码,该代码执行以下操作:

我有~100,000个2-D数据点,我有一对间隔。第一个间隔不会改变(在此示例中介于0和1之间),第二个间隔不断变化 我想获得具有以下内容的实例/数据点:

1)第一个区间(0,1)内的x坐标值 2)第二个(变化的间隔)内的y坐标值

% firstCol is a ~100,000 rows, one column array; x-coordinate
% secondCol is also a ~100,000 rows, one column array; y-coordinate

% boundaries of my first interval
%
maxOfMyFirstInterval = 1;
minOfMyFirstInterval = 0;

% allIntervalsMax is a ~10,000 rows, one column, of maximum values
% allIntervalsMin is a ~10,000 rows, one column, of minimum values
% 
% The above two columns contain the changing pairs, so the first pair would be
% (allIntervalsMin(1), allIntervalsMax(1))
%

% pre-allocate array that will hold number of data-points that satisfy 
% my condition
%
numberOfInstances = zeros(length(allIntervalsMax),1);

tic

% This will get the instances that satisfy my first condition,
% x-coordinate between 0 and 1
%
a_first = find((firstCol <= maxOfMyFirstInterval) & ...
    (firstCol >= minOfMyFirstInterval));

% Loop through the list of second intervals
%
for jx = 1:length(allIntervalsMax)

    a_second = find((secondCol <= allIntervalsMax(jx)) & ...
        (secondCol >= allIntervalsMin(jx)));

    a_both = intersect(a_first, a_second);

    numberOfInstances(jx) = length(a_both);
end

toc

这样做的时间是~29秒,我想知道是否有更快的方式。

1 个答案:

答案 0 :(得分:3)

如果你不厌烦找到并交叉,你可能会加快速度。所以

a_first = (firstCol <= maxOfMyFirstInterval) & ...
    (firstCol >= minOfMyFirstInterval);

% Loop through the list of second intervals
%
for jx = 1:length(allIntervalsMax)

    a_second = (secondCol <= allIntervalsMax(jx)) & ...
        (secondCol >= allIntervalsMin(jx));

    a_both = a_first & a_second;

    numberOfInstances(jx) = sum(a_both);
end