2011-02-25 220 views
0

我是Matlab的新手,並試圖擺脫Java/C++習俗。
問題是「我如何擺脫這些循環。」
我試圖用nchoosek(n0,2)擺脫循環的一個,但另一個問題出現了。(nchoosek的性能)Matlab擺脫循環

<Matlab code> 
for j=2:n0 
    for i=1:j-1 
     %wij is the number of rows of A that have 1 at both column i and column j 
     %summing col i and j to find #of common 1's 
     wij = length(find((A(:,i)+A(:,j))==2)); 
     %store it 
     W(1,j)=wij; 
     %testing whether the intersection of any two columns is too large 
     if wij>= (1+epsilon)*u2; 
     %create and edge between col i j 
     end 
    end 
end 
</matlab Code> 

回答

1

我認爲A是隻有0和1的陣列。

然後,可以通過寫入

B = A'*A; %'# B(i,j) = length(find((A(:,i)+A(:,j))==2)) 

%# threshold 
largeIntersection = B >= u2; 

%# find i,j of large intersections 
[largeIJ(:,1),largeIJ(:,2)] = find(largeIntersection); 

%# make sure we only get unique i,j pairs 
largeIJ = unique(sort(largeIJ,2),'rows'); 
創建colums之間的「距離」 NcoI位逐NCOL陣列乙