1

I want to have the euclidean norm of the difference for every two columns in a matrix. So that for example Column1-Column3 has its euclidean norm in the final matrix at (1,3) and (3,1). I already have a code, but I would like to ask if there is a simpler and especially faster way to get this kind of a matrix.

A = rand(4)
B=zeros(size(A));
for i = 2:size(A,1)
   diffCol = A - [A(:,i:end),A(:,1:i-1)];
   normsCol= sqrt(sum(diffCol.^2, 1));
   B=B+diag(normsCol(1:end-i+1),i-1);
end
B = B + B'

Example for a random 4x4 Matrix:

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Gabriel
  • 189
  • 2
  • 10
  • Transpose the input array to feed to the solution code(s) from the linked question and at the end square-root the output to get the desired output here. – Divakar Jan 23 '16 at 12:24
  • Great, works really fine and is much faster than my code. – Gabriel Jan 23 '16 at 12:45

0 Answers0