How to compute only the diagonal of a matrix product in Octave?

我怕爱的太早我们不能终老 提交于 2020-01-20 01:47:05

问题


Is there a way in Octave to compute and store only the diagonal of a matrix product?

Basically like doing: vector = diag(A*B);

I don't care about any of the values of A*B except those on the diagonal. The matrix sizes are around 80k x 12 and 12 x 80k, so even if I didn't care about the speed/extra memory it simply wont fit in RAM.

Strange, since Octave is a package for huge data sets and diagonals are very important, so it should be possible.


回答1:


The first element in the diagonal is the scalar product of the first row of A with the first column of B. The second element in the diagonal is the scalar product of the second row of A with the second column of B.

In other words:

vector = sum(A.*B',2);



回答2:


This is how you could do it in MATLAB (probably similar to Octave syntax):

vector = sum(A.*B',2);

This will compute only the resulting diagonal of the operation A*B as a column vector vector.




回答3:


actually I think it's the dot product of the first row of A with the first column of B... the second diagonal element is the dot product of the second row and the second column... etc



来源:https://stackoverflow.com/questions/2301046/how-to-compute-only-the-diagonal-of-a-matrix-product-in-octave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!