MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   Matlab摆脱循环 (https://www.labfans.com/bbs/showthread.php?t=26432)

poster 2019-12-14 20:13

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

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

[B]回答:[/B]

我假设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');

[url=https://stackoverflow.com/questions/5117099]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 05:07

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.