Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 463
Chris H Avatar asked Feb 20 '10 04:02

Chris H


People also ask

What is the diagonal of a product matrix?

A diagonal matrix is defined as a square matrix in which all off-diagonal entries are zero. (Note that a diagonal matrix is necessarily symmetric.) Entries on the main diagonal may or may not be zero. If all entries on the main diagonal are equal scalars, then the diagonal matrix is called a scalar matrix.

How do you find the matrix in octave?

Matrices are entered row by row using the same syntax as for vectors. To interchange rows with columns, that is, to find the transpose of a vector or a matrix, use the apostrophe. For example, the command octave#:#> C = [4 7.5 -1]' will transform the row vector C = [4 7.5 -1] into a column vector.


2 Answers

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);
like image 114
Jonas Avatar answered Oct 18 '22 01:10

Jonas


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.

like image 41
gnovice Avatar answered Oct 18 '22 01:10

gnovice