33 views (last 30 days)

I have a sparse rectangular matrix, J, for which I want to compute:

>> H = J' * J;

It's a bit slow (transpose is taking 5s and the matrix multiplication 9s), and given this is a special and very common case of a transpose and multiply, I was wondering if MATLAB had a faster way, e.g. one which avoids an explicit transpose.

Azzi Abdelmalek
on 3 Nov 2014

Edited: Azzi Abdelmalek
on 3 Nov 2014

c=sparse(J);

H=full(c*c');

John D'Errico
on 3 Nov 2014

Sign in to comment.

Matt J
on 4 Nov 2014

Edited: Matt J
on 5 Nov 2014

I don't think there's anything available to accelerate an exact calculation of J'*J for general J. However, if you know in advance that J'*J happens to be banded to diagonals -k:k for small k (or if it can be approximated as such), then it might help to compute the 2*k+1 non-trivial diagonals individually. You can do so without transposition as below.

[m,n]=size(J);

k=2;

kc=k+1;

tic;

B=zeros(n);

B(:,kc)=sum(J.^2);

for i=1:k

tmp=sum(J(:,1:end-i).*J(:,i+1:end));

B(1:end-i,kc-i)=tmp;

B(i+1:end,kc+i)=tmp;

end

result=spdiags(B,-k:k,n,n);

toc;

Whether this is actually faster will probably depend on the specifics of J. If nothing else, it spares you the large memory consumption of holding wide sparse matrices such as J' in RAM

>> J=sparse(m,n); Jt=J'; whos J Jt

Name Size Bytes Class Attributes

J 3192027x3225 25824 double sparse

Jt 3225x3192027 25536240 double sparse

Replacing J'*J by a banded approximation is something I haven't tried myself with Gauss-Newton specifically, but the role of J'*J is already as an approximation there, so I think it could work. Other minimization algorithms tend to be robust to small errors in the derivatives.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 15 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247429

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247429

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247430

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247430

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247463

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247463

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247470

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247470

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247488

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247488

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247549

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247549

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247590

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247590

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247634

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247634

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247677

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247677

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247697

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_247697

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324501

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324501

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324511

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324511

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324513

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324513

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324516

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324516

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324554

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/161175-fastest-way-to-compute-j-j-where-j-is-sparse#comment_324554

Sign in to comment.