質問

I am trying to get all the count of all intersections available between all columns and column combinations.

%I have a matrix of overlaps something like this : 
colHeader = {'var1','var2','var3','var4','var5'};
rowHeader = {'type1','type2','type3','type4','type5','type6','type7'};
overlap = [1,1,1,1,0;0,0,0,1,1;1,1,0,1,0;0,0,1,1,0;0,1,0,1,1;0,1,1,1,0;1,0,0,1,0];
%now i would like to get the count of overlap for all the columns variations (i.e. var1&var2 ... 
%var5&var1 at the first level, at the second level (var1&var2)&var3 etc. ) 
%the output in this case for level 1 and 2 is simple enough 
f = @(a,b) a&b
mat= zeros(5,5);
for i=1:5
    for j=1:5
       mat(i,j) = sum(f(overlap(:,i),overlap(:,j)));
    end
end
%      3     2     1     3     0
%      2     4     2     4     1
%      1     2     3     3     0
%      3     4     3     7     2
%      0     1     0     2     2
% where the diagonal is the first level of overlap and the rest are the relationships between the 
% different variables 
% i can continue in this fashion but not only is this ugly, it becomes not practical when dealing        
%with 
% bigger matrixes 
% So the problem is how to implement this for a big binary matrix in a manner that will return all 
% levels of intersection ? 
役に立ちましたか?

解決

temp = 1;
for level = 1:size(overlap,2)
    temp = bsxfun(@and, temp, permute(overlap,[1 3:1+level 2]));
    result{level} = squeeze(sum(temp));
end

How the result is interpreted

The variable result is a cell array which contains the results for all levels. Let n denote the number of columns in overlap.

  • Level 1: result{1} is a 1 x n vector which gives the intersection of each column of overlap with itself (i.e. the sum of each column). For example, result{1}(4) is the number of ones in column 4 of overlap.

  • Level 2: result{2} is an n x n matrix. For example, result{2}(4,2) is the intersection of columns 4 and 2 of overlap. (result{2} is mat in the original post).

  • Level 3: result{3} is an n x n x n array. For example, result{3}(4,2,5) is the intersection of columns 4, 2 and 5 of overlap.

  • [...] until level n.

How the code works

When computing the result at a given level, the code makes use of intermediate results from the previous level. This can be done because the "and" operation is associative. For example, at level 3, overlap(:,k) & overlap(:,m) & overlap(:,p) can be computed as (overlap(:,k) & overlap(:,m)) & overlap(:,p), where overlap(:,k) & overlap(:,m) was already computed (and stored) at level 2.

The final result at each level (result{level}) will be obtained as a column-wise sum. However, the intermediate result before that sum is stored (variable temp) to be re-used at the next level.

Each new level takes the intermediate result from the preceding level (temp), adds a new dimension (permute) and computes (bsxfun) the new intermediate result (new value of temp, with one more dimension). That intermediate result, upon column-wise sum (and squeeze to remove singleton dimensions), gives that level's final result (result{level}).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top