1
votes

I have a matrix X and use it to compute the square matrix in MATLAB: S=X*X'. I need to find only off-diagonal elements of S.

I understand how to do this for the diagonal: sum(X.*X,1). Is there the similar way to find off-diagonal elements by vectorization?

1
Welcome to the site! With S=X*X' you get the off-diagonal and the diagonal entries. If X has n rows, avoiding to compute the diagonal terms would only save n terms out of n^2 in total. You could save an additional factor of 2 due to symmetry. Is it worth it? What size is your matrix?Luis Mendo

1 Answers

0
votes

To get the elements of the d-th off-diagonal this should work:

sum(X(:,1:end-d).*X(:,1+d:end),1)

This is because for R=X*X', the d-th off-diagonal has these elements:

R(k,k+d) = sum (X(k,:) .* X(k+d,:))