There are several ways in Matlab to calculate "LU decomposition". Here is one:
function [L,A]=LU_factor(A,n)
L=eye(n);
for k=1:n
if (A(k,k) == 0) Error('Pivoting is needed!'); end
L(k+1:n,k)=A(k+1:n,k)/A(k,k);
for j=k+1:n
A(j,:)=A(j,:)-L(j,k)*A(k,:);
end
end
But my teacher told us that using for
in MATLAB may decrease the efficiency of the program. He told us to calculate the LU decomposition using fewer for
s. He said that you can find the required indexes without using for
and then with some tricks you won't need to use for
at all.
My first question is: will using for
really decrease the speed of the program? My second question is: how can I store the required indexes in an array and use them instead of a for
loop?