This example with norm
and random data
A = randn(2000,2000);
tic;
n1 = norm(A)
toc;
gives
n1 = 89.298
Elapsed time is 2.16777 seconds.
You can try eigs
to find only one (the largest) eigenvalue of the symmetric matrix A'*A
(or A*A'
if it is smaller for A
rectangular). It uses a Lanczos iteration method.
tic;
B = A'*A; % symmetric positive-definite. B = A*A' if it is smaller
n2 = sqrt(eigs(B, 1)),
toc
it outputs:
n2 = 89.298
Elapsed time is 0.311942 seconds.
If you don't want to use norm
or eigs
, and your matrix A
has good properties (singular values properly separated), you can try to approximate it with a power iteration method:
tic;
B = A'*A; % or B = A*A' if it is smaller
x = B(:,1); % example of starting point, x will have the largest eigenvector
x = x/norm(x);
for i = 1:200
y = B*x;
y = y/norm(y);
% norm(x - y); % <- residual, you can try to use it to stop iteration
x = y;
end;
n3 = sqrt(mean(B*x./x)) % translate eigenvalue of B to singular value of A
toc
which for the same random matrix (not particularly good properties) gives a ~0.1%
accurate solution:
n3 = 89.420
Elapsed time is 0.428032 seconds.
s(0,0)
is invalid MATLAB syntax. Did you even run this code? – Adriaannorm(A,2)
? In general, if Matlab has a built-in function it's about the fastest you can get, especially for matrix algebra – Luis Mendonorm
in terms of precision or speed – Luis Mendo