1

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

我有大约 100,000 个二维数据点,并且我有成对的间隔。第一个间隔不变(在本例中为 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 秒,我想知道是否有更快的方法。

4

1 回答 1

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
于 2012-06-19T07:07:53.267 回答