【问题标题】:Matlab get rid of loopsMatlab摆脱循环
【发布时间】:2011-02-25 12:16:25
【问题描述】:

我是 Matlab 的新手,正在尝试摆脱 Java/C++ 习惯。
问题是“我如何才能摆脱这些 for 循环。”
我尝试使用 nchoosek(n0,2) 摆脱其中一个循环,但出现了另一个问题。(nchoosek 的性能)

<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>

【问题讨论】:

    标签: performance matlab for-loop combinations sparse-matrix


    【解决方案1】:

    我假设A 是一个只有 0 和 1 的数组。

    然后你可以创建一个 nCol-by-nCol 数组 B 与列之间的“距离”通过编写

    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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多