2

我有两个矩阵 A 和 B,都包含事件开始和停止时间的列表:

A(i,1) = onset time of event i
A(i,2) = offset time of event i
B(j,1) = onset of event j
...

我的目标是获得两个不合理的列表,aIdx并且包含重叠的事件集。bIdxA(aIdx,:)B(bIdx,:)

我整天都在挠头,试图弄清楚这一点。有没有一种快速、简单、matlaby 的方法来做到这一点?

我可以使用 for 循环来做到这一点,但这对于 matlab 来说似乎有点 hacky:

aIdx = [];
bIdx = []
for i=1:size(A,1)
    for j=i:size(B,1)
        if overlap(A(i,:), B(j,:)) % overlap is defined elsewhere
            aIdx(end+1) = i;
            bIdx(end+1) = j;
        end
    end
end
4

4 回答 4

3

这是一个零循环解决方案:

overlap = @(x, y)y(:, 1) < x(:, 2) & y(:, 2) > x(:, 1)
[tmp1, tmp2] = meshgrid(1:size(A, 1), 1:size(B, 1));
M = reshape(overlap(A(tmp1, :), B(tmp2, :)), size(B, 1), [])';
[aIdx, bIdx] = find(M);
于 2012-04-24T19:40:48.053 回答
1

你可以用一个循环来做到这一点:

aIdx = false(size(A,1),1);
bIdx = false(size(B,1),1);
for k = 1:size(B,1)
    ai = ( A(:,1) >= B(k,1) & A(:,1) <= B(k,2) ) | ...
           ( A(:,2) >= B(k,1) & A(:,2) <= B(k,2) );
    if any(ai), bIdx(k) = true; end
    aIdx = aIdx | ai;
end

有一种方法可以创建矢量化算法。(我之前写过一个类似的函数,但现在找不到。)一个简单的工作流程是(1)结合两个矩阵,(2)创建一个索引来指示每个事件的来源,(3)创建一个矩阵来指示开始和停止位置,(4) 向量化和排序,(5) 查找与 diff、cumsum 或组合的重叠。

于 2012-04-24T18:05:12.537 回答
1
overlap_matrix = zeros(size(A,1),size(B,1))

for jj = 1:size(B,1)
    overlap_matrix(:,jj) = (A(:,1) <= B(jj,1)).*(B(jj,1) <= A(:,2));       
end

[r,c] = find(overlap_matrix)
% Now A(r(i),:) overlaps with B(c(i),:)

% Modify the above conditional if you want to also check
% whether events in A start in-between the events in B
% as I am only checking the first half of the conditional
% for simplicity.
于 2012-04-24T19:02:02.723 回答
0

完全矢量化的代码,没有 repmat 或 reshape(希望也更快)。我假设如果将完整的 A(req_indices,:) 和 B(req_indices,:) 对输入给它,函数“overlap”可以给出一个 1 和 0 的向量。如果重叠可以返回向量输出,则可以如下执行向量化。

设A矩阵中的行为Ra,B矩阵中的行为Rb,

AA=1:Ra;
AAA=AA(ones(Rb,1),:);
AAAA=AAA(:); % all indices of A arranged in desired format, i.e. [11...1,22..2,33...3, ...., RaRa...Ra]'

BB=(1:Rb)';
BBB=BB(:,ones(Ra,1)); 
BBBB=BBB(:);% all indices of B arranged in desired format, i.e. [123...Rb, 123...Rb,....,123...Rb]'

% Use overlap function
Result_vector = overlap(A(AAAA,:), B(BBBB,:));
Result_vector_without_zeros = find(Result_vector);
aIdx = AAAA(Results_vector_without_zeros);
bIdx = BBBB(Results_vector_without_zeros);

缺点:较大矩阵的内存消耗过多

于 2012-04-24T20:19:05.227 回答