-1

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.

Dan
  • 45,079
  • 17
  • 88
  • 157
fatima
  • 29
  • 4
  • 2
    This question is not clear. What do you mean by "i dont know how to make the condition that k must be different to i and different to j"? Could you give an example with some sample input and some sample output? – Unapiedra Oct 09 '14 at 08:44
  • the condition to give 1 (say that there is a topological link between i and j) is that Z(i,j)<= max(Z(i,k),Z(j,k)) for all k, I want to exclude the case when k is equal to i or equal to j – fatima Oct 09 '14 at 08:57
  • How about adding an example for a four by four input matrix and what you want as an output? – Unapiedra Oct 09 '14 at 09:11
  • It works with ~isequal(i,k) && ~isequal(j,k), thank you very much – fatima Oct 09 '14 at 09:38
  • @fatima note that according to [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan's_laws) `~(k==i) && ~(k==j)` is the same as `~(k==i || k==j)`. Use whichever makes the most intuitive sense to you – Dan Oct 09 '14 at 09:50
  • i dont know why it doesn't work with ~(k==i) && ~(k==j) or ~(k==i || k==j) , i have the error msg variable k might be set by a nonscalar operator. there was no error with ~isequal(i,k) && ~isequal(j,k), is it true, can i use it – fatima Oct 09 '14 at 10:04

1 Answers1

0
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
Dan
  • 45,079
  • 17
  • 88
  • 157
  • 1
    I get an error: "Operands to the || and && operators must be convertible to logical scalar values." Is there maybe a `for` missing in front of the `k=1:n` and the `;` removed? – Unapiedra Oct 09 '14 at 09:47