title: Avoid for loop for setting the matrix element in Matlab
body: <p>In a matrix, how does one set an adjacent and diagonal element to 1 if the values in these locations are the same avoiding a for loop? 
   An attempt made with for loop is given</p>
  
  <pre><code>[r,c] = size(mat1);
  Sval = zeros(size(mat1));
  for i = 1:r
      for j = 1:c-1
          if(mat1(i,j) == mat1(i,j+1))
              Sval(i,j) = 1;
              Sval(i,j+1) = 1;
          else
              Sval(i,j) = 0;
              Sval(i,j+1) = 0;
          end;
      end;
  end;
  </code></pre>