title: How to find row indices of large row matrix with n common elements in matlab?
body: <p>I am trying to find pairs of rows which index a matrix with 2 common elements. I have a triangulation of a 3D object and want to filter the triangles by the angle of neighboring triangles. So, I have to find the triangles that share an edge. In order to do this, I must find the rows of the .tri file that have 2 common points. </p>
  
  <p>I have 350ish .tri files each of them have about 7000x3 in dim. I have found this resource:
  <a href="http://www.mathworks.com/matlabcentral/answers/1853-search-nx3-array-row-wise-for-2-equal-elements/" rel="nofollow" title="mathworks link">MathWorks link</a>
  The accepted answer takes my mac air 15 mins, and Matt Figs answer takes about 8 mins per .tri file (I have 350!).</p>
  
  <p>How can I complete this process? I have been told to try and code this part in C ( I have never used C). I also have considered just trying to install Matlab on a linux server and run it there ( I have never done that either). Any advice on how to either write a program in C or use an AWS server?</p>
  
  <p>3D files located here: <a href="http://tosca.cs.technion.ac.il/book/resources_data.html" rel="nofollow">3D files</a>
  Here is the code I run:
  (any suggestions to clean it up would also be appreciated)</p>
  
  <pre><code>addpath('/Users/len/Desktop/javaplex/nonrigid3d')
  files1 = dir('/Users/len/Desktop/javaplex/nonrigid3d/*.tri');    
  files2 = dir('/Users/len/Desktop/javaplex/nonrigid3d/*.vert');
  
  time=cputime;
  for k =1:length(files1)    
      X=load(files1(k).name);
      Pt=load(files2(k).name);
  
      %define a sparse matrix for holding angles between 2-simplices
      W=sparse(length(X),length(X));
  
      % Find common vertices: try with C
      Xs = sort(X,2);
      S = size(X,1);
      L = S*(S+1)/2;
      F = sparse(L,L);
      cnt = 0;
      Ntoshare = 2;
      for ii = 1:S
          for jj = ii+1:S
              cnt = cnt + 1;
              if sum(ismembc(Xs(ii,:),Xs(jj,:)))==Ntoshare
                  F(jj,ii) = 1;
              end
          end
  
      end
  
  [I,J] = find(F); % pairs of rows that have 2 common vertices.
  cmn=[I,J];
  
  % Calculate angle and store in sparse matrix. row and col reference triangles
  for i=1:length(cmn)
      normal1=cross(Pt(X(cmn(i,1),1),:)-Pt(X(cmn(i,1),2),:) ,Pt(X(cmn(i,1),1),:)-Pt(X(cmn(i,1),3),:));
  
      normal2=cross(Pt(X(cmn(i,2),1),:)-Pt(X(cmn(i,2),2),:), Pt(X(cmn(i,2),1),:)-Pt(X(cmn(i,2),3),:));
  
      theta = acos((dot(normal1,normal2)/(norm(normal1)*norm(normal2))));
  
      W(cmn(i,1),cmn(i,2))=theta;
  end   
  
  % save W as a txt file as nameofimage.txt in current directory
  [i,j,val]=find(W);
  data_dump=[i,j,val];
  l=files1(k).name;
  filename = strrep(l, '.tri','.txt');
  dlmwrite(filename, data_dump, 'delimiter',' ');
  </code></pre>