<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182</link>
    <title>MATLAB Central Newsreader - vectorize intersect with cells?</title>
    <description>Feed for thread: vectorize intersect with cells?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.nl/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 09 Mar 2009 01:04:42 +0000</pubDate>
      <title>vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633386</link>
      <author>HenryW</author>
      <description>I have a cell   cliques=cell(n,1)  where each cell &lt;br&gt;
cliques{i} is a set of integers  e.g.  cliques{1}=[1 3 9 29]&lt;br&gt;
I would like to vectorize this for loop  ---- which takes very long:&lt;br&gt;
&lt;br&gt;
for ic=1:n-1,&lt;br&gt;
&amp;nbsp;&amp;nbsp;for jc=ic+1:n,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;intcliques(ic,jc)=length(intersect(cliques{ic},cliques{jc}));&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
any suggestions?</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 07:56:01 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633422</link>
      <author>Bruno Luong</author>
      <description>HenryW &amp;lt;hwolkowicz@uwaterloo.ca&amp;gt; wrote in message &amp;lt;18230588.1236560716722.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; I have a cell   cliques=cell(n,1)  where each cell &lt;br&gt;
&amp;gt; cliques{i} is a set of integers  e.g.  cliques{1}=[1 3 9 29]&lt;br&gt;
&amp;gt; I would like to vectorize this for loop  ---- which takes very long:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for ic=1:n-1,&lt;br&gt;
&amp;gt;   for jc=ic+1:n,&lt;br&gt;
&amp;gt;     intcliques(ic,jc)=length(intersect(cliques{ic},cliques{jc}));&lt;br&gt;
&amp;gt;   end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; any suggestions?&lt;br&gt;
&lt;br&gt;
Could you tell us the typical values of n and length(cliques{i})?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 09:28:30 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633433</link>
      <author>us</author>
      <description>HenryW wrote:&lt;br&gt;
&amp;gt; I would like to vectorize this for loop =A0---- which takes very long:&lt;br&gt;
&amp;gt; any suggestions...&lt;br&gt;
&lt;br&gt;
this loop cannot be vectorized (in the true sense of vectorization)...&lt;br&gt;
it only can be optimized...&lt;br&gt;
&lt;br&gt;
two of the possible solutions including simple profiling&lt;br&gt;
- copy/paste&lt;br&gt;
&lt;br&gt;
% the data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear c* nc* r*;     % &amp;lt;- save old stuff!&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nt=3D10;     % &amp;lt;- #runs/solution&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nc=3D100;     % &amp;lt;- max #data/cell&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cc=3Dcell(nc,1);&lt;br&gt;
for i=3D1:nc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nx=3Dceil(10*rand(1,nc));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ne=3Dceil(nc*rand);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cc{i,1}=3Dnx(1:ne);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% the engine&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r1=3Dzeros(nc,nc);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r2=3Dzeros(nc,nc);&lt;br&gt;
% - sol #1 =3D OP&lt;br&gt;
tic;&lt;br&gt;
for  i=3D1:nt&lt;br&gt;
for  ic=3D1:nc-1,&lt;br&gt;
for  jc=3Dic+1:nc,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r1(ic,jc)=3Dlength(intersect(cc{ic},cc{jc}));&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
% - sol #2&lt;br&gt;
tic;&lt;br&gt;
for  i=3D1:nt&lt;br&gt;
for  ic=3D1:nc-1,&lt;br&gt;
for  jc=3Dic+1:nc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r2(ic,jc)=3Dsum(ismembc(unique(cc{ic}),sort(cc{jc})));&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
% - sol #3&lt;br&gt;
tic;&lt;br&gt;
for  i=3D1:nt&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ix=3Dnchoosek(1:nc,2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r3=3Dcellfun(@(x,y) sum(ismembc(unique(x),sort(y))),cc(ix(:,1)),cc&lt;br&gt;
(ix(:,2)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r3=3Daccumarray(ix,r3,[nc,nc]);&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(isequal(r1,r2,r3));&lt;br&gt;
%{&lt;br&gt;
% wintel system&lt;br&gt;
% ic2/2*2.6ghz/2gb/winxp.sp3/r2008b&lt;br&gt;
Elapsed time is 5.368212 seconds.     % &amp;lt;- sol #1 =3D OP&lt;br&gt;
Elapsed time is 2.040264 seconds.     % &amp;lt;- sol #2 =3D ~50%&lt;br&gt;
Elapsed time is 2.102942 seconds.     % &amp;lt;- sol #3 =3D ~50%&lt;br&gt;
isequal =3D 1&lt;br&gt;
%}&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 09:33:50 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633434</link>
      <author>us</author>
      <description>HenryW wrote:&lt;br&gt;
&amp;gt; I would like to vectorize this for loop  ---- which takes very long:&lt;br&gt;
&amp;gt; any suggestions...&lt;br&gt;
&lt;br&gt;
this loop cannot be vectorized (in the true sense of&lt;br&gt;
vectorization)...&lt;br&gt;
it only can be optimized...&lt;br&gt;
&lt;br&gt;
two of the possible solutions including simple profiling&lt;br&gt;
- copy/paste&lt;br&gt;
- second post via google...&lt;br&gt;
&lt;br&gt;
% the data&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;clear c* nc* r*;     % &amp;lt;- save old stuff!&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nt=10;     % &amp;lt;- #runs/solution&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nc=100;     % &amp;lt;- max #data/cell&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cc=cell(nc,1);&lt;br&gt;
for i=1:nc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nx=ceil(10*rand(1,nc));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ne=ceil(nc*rand);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cc{i,1}=nx(1:ne);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
% the engine&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r1=zeros(nc,nc);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r2=zeros(nc,nc);&lt;br&gt;
% - sol #1 = OP&lt;br&gt;
tic;&lt;br&gt;
for  i=1:nt&lt;br&gt;
for  ic=1:nc-1,&lt;br&gt;
for  jc=ic+1:nc,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r1(ic,jc)=length(intersect(cc{ic},cc{jc}));&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
% - sol #2&lt;br&gt;
tic;&lt;br&gt;
for  i=1:nt&lt;br&gt;
for  ic=1:nc-1,&lt;br&gt;
for  jc=ic+1:nc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r2(ic,jc)=sum(ismembc(unique(cc{ic}),sort(cc{jc})));&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
% - sol #3&lt;br&gt;
tic;&lt;br&gt;
for  i=1:nt&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ix=nchoosek(1:nc,2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r3=cellfun(@(x,y) sum(ismembc(unique(x),sort(y))),...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cc(ix(:,1)),cc(ix(:,2)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r3=accumarray(ix,r3,[nc,nc]);&lt;br&gt;
end&lt;br&gt;
toc&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp(isequal(r1,r2,r3));&lt;br&gt;
%{&lt;br&gt;
% wintel system&lt;br&gt;
% ic2/2*2.6ghz/2gb/winxp.sp3/r2008b&lt;br&gt;
Elapsed time is 5.368212 seconds.     % &amp;lt;- sol #1 = OP&lt;br&gt;
Elapsed time is 2.240264 seconds.     % &amp;lt;- sol #2&lt;br&gt;
Elapsed time is 2.302942 seconds.     % &amp;lt;- sol #3&lt;br&gt;
isequal = 1&lt;br&gt;
%}&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 10:33:30 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633445</link>
      <author>Bruno Luong</author>
      <description>Here is one way, complicated but should be much faster at the run&lt;br&gt;
time:&lt;br&gt;
&lt;br&gt;
function testintersect&lt;br&gt;
&lt;br&gt;
n=200; % number of sets&lt;br&gt;
m=100; % element value are from 0 to m-1&lt;br&gt;
meannumel=50; % average number of elements in lists&lt;br&gt;
s = 20; % standard deviation of number of elements&lt;br&gt;
cliques = gendata(n, m, meannumel, s);&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
%%%&lt;br&gt;
% Engine 1 (Henry)&lt;br&gt;
tic&lt;br&gt;
intcliques1=Engine1(cliques);&lt;br&gt;
toc % 2.374056 seconds.&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
%%%&lt;br&gt;
% Engine 3 (Bruno)&lt;br&gt;
tic&lt;br&gt;
intcliques3=Engine3(cliques);&lt;br&gt;
toc % 0.059586 seconds.&lt;br&gt;
&lt;br&gt;
% Check&lt;br&gt;
isequal(intcliques1,intcliques3)&lt;br&gt;
subplot(1,2,1);&lt;br&gt;
imagesc(intcliques1);&lt;br&gt;
subplot(1,2,2);&lt;br&gt;
imagesc(intcliques3);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% Generate data&lt;br&gt;
function cliques = gendata(n, m, meannumel, s)&lt;br&gt;
% Data&lt;br&gt;
&lt;br&gt;
cliques=cell(1,n);&lt;br&gt;
for k=1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nelk = meannumel+round(s*randn);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nelk = min(max(nelk,1),m);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cliques{k} = floor(m*rand(1,nelk));&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
function intcliques=Engine1(cliques)&lt;br&gt;
n=length(cliques);&lt;br&gt;
intcliques=zeros(n,n);&lt;br&gt;
for ic=1:n-1,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for jc=ic+1:n,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;intcliques(ic,jc)=length(intersect(cliques{ic},cliques{jc}));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
function intcliques=Engine3(cliques)&lt;br&gt;
n=length(cliques);&lt;br&gt;
% Transform to long column array&lt;br&gt;
c = cellfun(@(x) unique(x(:)), cliques, 'uni', false);&lt;br&gt;
allc = cat(1,c{:});&lt;br&gt;
% Take a unique&lt;br&gt;
[u I J]=unique(allc);&lt;br&gt;
% map c to index integer set: 1,2,...&lt;br&gt;
cmap = mat2cell(J,cellfun(@length,c),1);&lt;br&gt;
% Set id, same size as cmap&lt;br&gt;
setid = arrayfun(@(n) n+zeros(size(cmap{n})), 1:length(cmap), ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'uni', false);&lt;br&gt;
% Reshape as long vectors&lt;br&gt;
cmap = cat(1,cmap{:});&lt;br&gt;
setid = cat(1,setid{:});&lt;br&gt;
&lt;br&gt;
% For each element, return the list of set that contain it&lt;br&gt;
contained=accumarray(cmap,setid,[],@(id) {sort(id)});&lt;br&gt;
&lt;br&gt;
% Build the list of all couples&lt;br&gt;
couples = cellfun(@allcouples, contained, 'uni', false);&lt;br&gt;
couples = cat(1,couples{:});&lt;br&gt;
% Count&lt;br&gt;
%intcliques=sparse(couples(:,1),couples(:,2),1,n,n); % &amp;lt; sparse&lt;br&gt;
intcliques=accumarray(couples,1,[n,n]);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;function res=allcouples(k) % Return all couples of a list&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;k=k(:);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[I J]=ndgrid(1:numel(k),1:numel(k));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[I J]=deal(I(I&amp;lt;J),J(I&amp;lt;J));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;res=[k(I(:)) k(J(:))];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 15:26:02 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633511</link>
      <author>Bruno Luong</author>
      <description>% More compact code:&lt;br&gt;
&lt;br&gt;
function testintersect&lt;br&gt;
&lt;br&gt;
n=200; % number of sets&lt;br&gt;
m=100; % element value are from 0 to m-1&lt;br&gt;
meannumel=50; % average number of elements in lists&lt;br&gt;
s = 20; % standard deviation of number of elements, 0 to m-1&lt;br&gt;
cliques = gendata(n, m, meannumel, s);&lt;br&gt;
&lt;br&gt;
% Engine 1 (Henry)&lt;br&gt;
tic&lt;br&gt;
intcliques1=Engine1(cliques);&lt;br&gt;
toc % 2.373781 seconds.&lt;br&gt;
&lt;br&gt;
% Engine 3 (Bruno)&lt;br&gt;
tic&lt;br&gt;
intcliques3=Engine3(cliques);&lt;br&gt;
toc % 0.048709 seconds.&lt;br&gt;
&lt;br&gt;
% Check&lt;br&gt;
isequal(intcliques1,intcliques3)&lt;br&gt;
subplot(1,2,1);&lt;br&gt;
imagesc(intcliques1);&lt;br&gt;
subplot(1,2,2);&lt;br&gt;
imagesc(intcliques3);&lt;br&gt;
&lt;br&gt;
end % testintersect&lt;br&gt;
&lt;br&gt;
% Generate data&lt;br&gt;
function cliques = gendata(n, m, meannumel, s)&lt;br&gt;
% Data&lt;br&gt;
&lt;br&gt;
cliques=cell(1,n); % preallocate&lt;br&gt;
for k=1:n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% number of elements in set #k&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nelk = meannumel+round(s*randn);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nelk = min(max(nelk,0),m); % clip it&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% random set of length nelk&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cliques{k} = floor(m*rand(1,nelk));&lt;br&gt;
end&lt;br&gt;
end % gendata&lt;br&gt;
&lt;br&gt;
function intcliques=Engine1(cliques)&lt;br&gt;
n=length(cliques);&lt;br&gt;
intcliques=zeros(n,n);&lt;br&gt;
for ic=1:n-1,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for jc=ic+1:n,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;intcliques(ic,jc)=length(intersect(cliques{ic},cliques{jc}));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
end % Engine 1&lt;br&gt;
&lt;br&gt;
function intcliques=Engine3(cliques)&lt;br&gt;
% Number of sets&lt;br&gt;
n=length(cliques);&lt;br&gt;
% Transform to long column array&lt;br&gt;
c = cellfun(@(x) unique(x(:)), cliques, 'uni', false);&lt;br&gt;
% Set id, same size as c&lt;br&gt;
setid = arrayfun(@(id) id+zeros(size(c{id})), 1:n, ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'uni', false);&lt;br&gt;
% Reshape as long vectors   &lt;br&gt;
setid = cat(1,setid{:});   &lt;br&gt;
% Take a unique of all elements&lt;br&gt;
% map c to index integer set: 1,2,...&lt;br&gt;
[dummy dummy cmap]=unique(cat(1,c{:}));&lt;br&gt;
clear dummy&lt;br&gt;
&lt;br&gt;
% For each element, return the list of set that contain it&lt;br&gt;
contained=accumarray(cmap(:),setid,[],@(id) {id});&lt;br&gt;
clear cmap setid&lt;br&gt;
&lt;br&gt;
% Build the list of all couples&lt;br&gt;
couples = cellfun(@allcouples, contained, 'uni', false);&lt;br&gt;
couples = cat(1,couples{:}); % Put in a long (p x 2)-array&lt;br&gt;
&lt;br&gt;
% Count&lt;br&gt;
%intcliques=sparse(couples(:,1),couples(:,2),1,n,n); % &amp;lt; sparse&lt;br&gt;
intcliques=accumarray(couples,1,[n n]); % &amp;lt;- full&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;function res=allcouples(k) % Return all couples of a list&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[K1 K2]=ndgrid(k(:),k(:));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;filter=K1&amp;lt;K2;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;res=[K1(filter) K2(filter)];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end % allcouples&lt;br&gt;
&lt;br&gt;
end % Engine3&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 16:11:01 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633523</link>
      <author>Roger Stafford</author>
      <description>HenryW &amp;lt;hwolkowicz@uwaterloo.ca&amp;gt; wrote in message &amp;lt;18230588.1236560716722.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; I have a cell   cliques=cell(n,1)  where each cell &lt;br&gt;
&amp;gt; cliques{i} is a set of integers  e.g.  cliques{1}=[1 3 9 29]&lt;br&gt;
&amp;gt; I would like to vectorize this for loop  ---- which takes very long:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for ic=1:n-1,&lt;br&gt;
&amp;gt;   for jc=ic+1:n,&lt;br&gt;
&amp;gt;     intcliques(ic,jc)=length(intersect(cliques{ic},cliques{jc}));&lt;br&gt;
&amp;gt;   end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; any suggestions?&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;The reason your nested for-loops take so long is that there are n*(n-1)/2 calls on the 'intersect' function and each time this function must do some sorting to find shared elements.  That's a lot of sorting for large n.  The following code does just one massive sort on the assumption that that will save unnecessary repetition in the sorting process.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Call your cell array 'cliques' just 'c' here and assume that each element of 'c' is a column vector and that there are n such vectors in the cell array.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;s = ones(n+1,1);&lt;br&gt;
&amp;nbsp;for ic = 1:n, s(ic+1) = s(ic) + length(c{ic}); end&lt;br&gt;
&amp;nbsp;m = s(n+1)-1;&lt;br&gt;
&amp;nbsp;x = zeros(m,1);&lt;br&gt;
&amp;nbsp;for ic = 1:n, x(s(ic):s(ic+1)-1) = c{ic}; end&lt;br&gt;
&amp;nbsp;[x,p] = sort(x); q = (1:m)'; q(p) = q;&lt;br&gt;
&amp;nbsp;u = cumsum([1;diff(x)~=0]);&lt;br&gt;
&amp;nbsp;t = cumsum(accumarray(s,1));&lt;br&gt;
&amp;nbsp;A = sparse(u(q),t(1:m),1,u(m),n);&lt;br&gt;
&amp;nbsp;A = full(A'*A);&lt;br&gt;
&lt;br&gt;
Then A is your desired n x n matrix (which you called 'intcliques'.)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;You will note that the diagonal and the lower triangular part are also filled.  I hope that is all right with you.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I used the 'sparse' function on the assumption that at this point A would be very sparsely filled.  If that is not so, you can replace it with an equivalent call to 'accumarray' and drop the 'full' in the next line.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I have also assumed that there are no repeated elements in each individual column vector of 'c'.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 17:03:56 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633535</link>
      <author>HenryW</author>
      <description>The values of n are typically from 2000 upwards; often 10,000. But, we are hoping to solve some very large problems and this is a preliminary preprocessing step.</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 17:50:02 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633551</link>
      <author>Bruno Luong</author>
      <description>Great algo from Roger, very quick using binary sparse matrix to store set-element relationship. Here is the algo, slightly modified to give the same result as the original one, and vectorized two for-oops:&lt;br&gt;
&lt;br&gt;
function A = Engine4(c) % Roger&lt;br&gt;
n=length(c);&lt;br&gt;
c = cellfun(@(x) unique(x(:)), c, 'uni', false);&lt;br&gt;
s = cumsum([1 cellfun(@length,c)]).';&lt;br&gt;
x=cat(1,c{:});&lt;br&gt;
m = s(n+1)-1;&lt;br&gt;
[x,p] = sort(x);&lt;br&gt;
q = (1:m)';&lt;br&gt;
q(p) = q;&lt;br&gt;
u = cumsum([1;diff(x)~=0]);&lt;br&gt;
t = cumsum(accumarray(s,1));&lt;br&gt;
A = sparse(u(q),t(1:m),1,u(m),n);&lt;br&gt;
A = triu(full(A'*A),1);&lt;br&gt;
end % Engine 4</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 20:09:01 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633590</link>
      <author>Bruno Luong</author>
      <description>% Somehow shorter&lt;br&gt;
&lt;br&gt;
function A = Engine5(c) % Roger &amp; Bruno&lt;br&gt;
&lt;br&gt;
c = cellfun(@(x) unique(x(:)), c, 'uni', false);&lt;br&gt;
l = cellfun(@length,c(:));&lt;br&gt;
setid = cumsum(accumarray(cumsum([1; l]),1));&lt;br&gt;
[dummy dummy cmap]=unique(cat(1,c{:}));&lt;br&gt;
A = sparse(setid(1:end-1),cmap,1,length(c),length(dummy));&lt;br&gt;
A = triu(full(A*A.'),1);&lt;br&gt;
&lt;br&gt;
end % Engine 5&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 20:36:01 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633602</link>
      <author>Roger Stafford</author>
      <description>"Bruno Luong" &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gp3ksa$aak$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Great algo from Roger, very quick using binary sparse matrix to store set-element relationship. Here is the algo, slightly modified to give the same result as the original one, and vectorized two for-oops:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function A = Engine4(c) % Roger&lt;br&gt;
&amp;gt; n=length(c);&lt;br&gt;
&amp;gt; c = cellfun(@(x) unique(x(:)), c, 'uni', false);&lt;br&gt;
&amp;gt; s = cumsum([1 cellfun(@length,c)]).';&lt;br&gt;
&amp;gt; x=cat(1,c{:});&lt;br&gt;
&amp;gt; m = s(n+1)-1;&lt;br&gt;
&amp;gt; [x,p] = sort(x);&lt;br&gt;
&amp;gt; q = (1:m)';&lt;br&gt;
&amp;gt; q(p) = q;&lt;br&gt;
&amp;gt; u = cumsum([1;diff(x)~=0]);&lt;br&gt;
&amp;gt; t = cumsum(accumarray(s,1));&lt;br&gt;
&amp;gt; A = sparse(u(q),t(1:m),1,u(m),n);&lt;br&gt;
&amp;gt; A = triu(full(A'*A),1);&lt;br&gt;
&amp;gt; end % Engine 4&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Thanks, Bruno, for fixing up that code for generating x and s.  I am a "babe in the woods" when it comes to cell arrays.  Also thanks for the correction with 'triu'.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;As it turns out, neither that inverse permutation 'q' nor the quantity 'm' were actually needed, so here is how your modification would look without them:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;n=length(c);&lt;br&gt;
&amp;nbsp;c = cellfun(@(x) unique(x(:)), c, 'uni', false);&lt;br&gt;
&amp;nbsp;s = cumsum([1 cellfun(@length,c)]).';&lt;br&gt;
&amp;nbsp;x=cat(1,c{:});&lt;br&gt;
&amp;nbsp;[x,p] = sort(x);&lt;br&gt;
&amp;nbsp;u = cumsum([1;diff(x)~=0]);&lt;br&gt;
&amp;nbsp;t = cumsum(accumarray(s,1));&lt;br&gt;
&amp;nbsp;A = sparse(u,t(p),1,u(end),n);&lt;br&gt;
&amp;nbsp;A = triu(full(A'*A),1);&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;I forgot to mention that this code worked with my tests even with some of the cell vectors empty.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;It is possible the 'unique' operation in your second line might be avoided by suitably altering the subsequent algorithm so as to achieve the same effect without the extra expense of the sorting involved in 'unique'.  Ideally, the one massive sort ought to do the whole job with the right procedure.  Any duplicate copies within a cell vector will appear in a contiguous string within x and there ought to be a way of excluding them from the count if they are treated the right way.  However, I haven't had time to look into that aspect of it.&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 20:40:17 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633608</link>
      <author>HenryW</author>
      <description>Rogers algorithm was really fast --- thanks!!&lt;br&gt;
But I could not get Bruno's last modification to work.</description>
    </item>
    <item>
      <pubDate>Mon, 09 Mar 2009 21:18:01 +0000</pubDate>
      <title>Re: vectorize intersect with cells?</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/246182#633612</link>
      <author>Bruno Luong</author>
      <description>"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gp3ujh$9k3$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   It is possible the 'unique' operation in your second line might be avoided by suitably altering the subsequent algorithm so as to achieve the same effect without the extra expense of the sorting involved in 'unique'.  Ideally, the one massive sort ought to do the whole job with the right procedure.  Any duplicate copies within a cell vector will appear in a contiguous string within x and there ought to be a way of excluding them from the count if they are treated the right way.  However, I haven't had time to look into that aspect of it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Roger,&lt;br&gt;
&lt;br&gt;
Do you think something along this line? Now we have just a unique call of unique!&lt;br&gt;
&lt;br&gt;
function A = Engine6(c) % Roger &amp; Bruno&lt;br&gt;
% c must be a list of *row* vectors&lt;br&gt;
[dummy dummy cmap]=unique([c{:}]);&lt;br&gt;
nele = cellfun(@length,c(:));&lt;br&gt;
setid = cumsum(accumarray(cumsum([1; nele]),1));&lt;br&gt;
A = sparse(setid(1:end-1),cmap(:),1,length(c),length(dummy));&lt;br&gt;
A = spones(A);&lt;br&gt;
A = triu(full(A*A.'),1);&lt;br&gt;
end % Engine 6&lt;br&gt;
&lt;br&gt;
It's run about 10% faster on my computer for n=1000, and average 50 elements for each set.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
  </channel>
</rss>
