English 中文(简体)
计算Mx2和Nx2矩阵中的重叠
原标题:Calculating overlap in Mx2 and Nx2 matrices
  • 时间:2012-04-24 16:07:09
  •  标签:
  • matlab

我有两个矩阵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和bIdx,如:A(aIdx,:B(bIdx,>包含一系列重叠的活动。

我每天都在赞扬我的头脑,试图把这个名字 figure出来。 是否有迅速、容易、巧妙地做到这一点?

我可以作假,但这种mat似乎为mat:

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
最佳回答

这里的解决办法是:

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);
问题回答

你们可以这样做:

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) 发现与扩散、cum或组合重叠。

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.

完全以病媒为本的代码,不重复使用或重复使用(同样更快)。 我已经假定,如果A(req_indices,:)和B(req_indices,:)的完全奶制品被配给它,那么“重叠”功能就能够提供1至0的病媒。 如果重叠能够回馈病媒的产出,那么病媒化可按下文所述进行。

Let rows in A matrix be Ra and Rows in B matrix be 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);

DISADVANTAGE: TOO MUCH RAMCONUMPTION FOR LARERT MATRICES





相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签