Matlab избавиться от петель - PullRequest
       11

Matlab избавиться от петель

0 голосов
/ 25 февраля 2011

Я новичок в Matlab и пытаюсь избавиться от обычаев Java / C ++.
Вопрос в том, "как я могу избавиться от них для циклов."), чтобы избавиться от одного из циклов, но возникла другая проблема. (производительность 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>

1 Ответ

1 голос
/ 25 февраля 2011

Я предполагаю, что A - это массив только с 0 и 1.

Затем вы можете создать массив B nCol-by-nCol с «расстоянием» между столбцами, написав

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