Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!v39g2000yqm.googlegroups.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: vectorize intersect with cells?
Date: Mon, 9 Mar 2009 02:33:50 -0700 (PDT)
Organization: http://groups.google.com
Lines: 66
Message-ID: <6e8d1934-f537-4fe9-9a46-4113d8b7d024@v39g2000yqm.googlegroups.com>
References: <18230588.1236560716722.JavaMail.jakarta@nitrogen.mathforum.org> 
	<052bc662-9c85-44dc-9091-d010c6bedaec@y13g2000yqn.googlegroups.com>
NNTP-Posting-Host: 130.60.28.29
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1236591230 1128 127.0.0.1 (9 Mar 2009 09:33:50 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 9 Mar 2009 09:33:50 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: v39g2000yqm.googlegroups.com; posting-host=130.60.28.29; 
	posting-account=ZMHqCRAAAABayS39QQZoXE1x9uDhXZ3G
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 
	Trident/4.0; MathPlayer 2.10d; GTB6; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 
	3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 
	3.5.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:523427

HenryW wrote:
> I would like to vectorize this for loop  ---- which takes very long:
> any suggestions...

this loop cannot be vectorized (in the true sense of
vectorization)...
it only can be optimized...

two of the possible solutions including simple profiling
- copy/paste
- second post via google...

% the data
     clear c* nc* r*;     % <- save old stuff!
     nt=10;     % <- #runs/solution
     nc=100;     % <- max #data/cell
     cc=cell(nc,1);
for i=1:nc
     nx=ceil(10*rand(1,nc));
     ne=ceil(nc*rand);
     cc{i,1}=nx(1:ne);
end

% the engine
     r1=zeros(nc,nc);
     r2=zeros(nc,nc);
% - sol #1 = OP
tic;
for  i=1:nt
for  ic=1:nc-1,
for  jc=ic+1:nc,
     r1(ic,jc)=length(intersect(cc{ic},cc{jc}));
end
end
end
toc
% - sol #2
tic;
for  i=1:nt
for  ic=1:nc-1,
for  jc=ic+1:nc
     r2(ic,jc)=sum(ismembc(unique(cc{ic}),sort(cc{jc})));
end
end
end
toc
% - sol #3
tic;
for  i=1:nt
     ix=nchoosek(1:nc,2);
     r3=cellfun(@(x,y) sum(ismembc(unique(x),sort(y))),...
                cc(ix(:,1)),cc(ix(:,2)));
     r3=accumarray(ix,r3,[nc,nc]);
end
toc
     disp(isequal(r1,r2,r3));
%{
% wintel system
% ic2/2*2.6ghz/2gb/winxp.sp3/r2008b
Elapsed time is 5.368212 seconds.     % <- sol #1 = OP
Elapsed time is 2.240264 seconds.     % <- sol #2
Elapsed time is 2.302942 seconds.     % <- sol #3
isequal = 1
%}

us