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: