How to compute diagonal entries of a matrix of inner products (gram matrix) in a vectorized way?

8 ビュー (過去 30 日間)
I am interested in computing only the diagonals of a matrix of inner products between vectors (Gram matrix). Say that we have the Dx1 vectors t_i stacked in matrix as follows t = [t_1, ..., t_k, ..., t_K]. I want to computed only the diagonal as in:
inner_product = t_i' * t_i
One obvious way to do it is to do:
Gram = t' * t
and then extract the diagonal entries Gram(i,i) but it seems that matlab is doing a lot of unnecessary computation (computing the off diagonals) and I wanted to avoid this using vectorized code. One can obviously also implemented this with a for loop and iterate through the matrix, however, since I have large vectors and I have the resources to store the data in GPUs, it seems silly to me do such a for loop when I can just take advantage of vectorized code.
Is it possible to compute only the diagonal entries of the gram matrix using vectorized code?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 5 日
編集済み: Azzi Abdelmalek 2016 年 4 月 5 日
out=sum(A.*A,2)
  1 件のコメント
Brando Miranda
Brando Miranda 2016 年 5 月 2 日
編集済み: Brando Miranda 2016 年 5 月 2 日
I think you mean sum(A.*A,1), since each column of A is the vector in question.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by