Search code examples
matlabfor-loopadjacency-matrix

Make sure nested loop variables have different values


I want to create an adjacency matrix from another metric matrix in Matlab. My program is as follow:

function [V] = adjacency(Z)
n= size(Z,1);
V = zeros(n);
k=1:n;
  for i = 1:n 
    for j = 1:n 
      if Z(i,j)<= max(Z(i,k),Z(j,k)) 
       V(i,j)=1;
       V(j,i)=1;
      else
       V(i,j)=0;
       V(j,i)=0;
      end
    end
  end
end

I don't know how to make the condition that k must be different to i and different to j.


Solution

  • for k=1:n;
      for i = 1:n 
        for j = 1:n 
    
          if(~(k==i || k==j))
    
              if Z(i,j)<= max(Z(i,k),Z(j,k)) 
                V(i,j)=1;
                V(j,i)=1;
              else
                V(i,j)=0;
                V(j,i)=0;
              end
    
          end
    
        end
      end
    end