|
wahyoe Unggul wrote:
> I have a problem with the function below, if you call the large matrix
> requires a long time, so that the process of execution to be slow, can
> you help me modify the function to be able to execute quickly
>
> function [A]=assembly
>
> load scratch_file.txt;
> tA=scratch_file;clear scratch_file;
> m=max(tA(:,2));
> A=zeros(m,m);
> %B=zeros(size(tA));
> B=A;C=A;
> for i=1:length(tA);
> C=A;
> B(tA(i,2),tA(i,3))=tA(i,1);
> A(tA(i,2),tA(i,3))=B(tA(i,2),tA(i,3))+C(tA(i,2),tA(i,3));
> end;
Is it correct that you do not care what the values of B and C are after the
loop? If so then your loop becomes
for i = 1:length(tA)
A(tA(i,2),tA(i,3)) = tA(i,1) + A(tA(i,2),tA(i,3));
end
Which can be optimized to a plain vector calculation as
T = sub2ind(size(A),tA(i,2),tA(i,3));
A(T) = A(T) + tA(:,1);
If, though, the tA(i,2),tA(i,3) pairs might be duplicated, then
T = sub2ind(size(A),tA(i,2),tA(i,3));
T1 = accumarray(T,tA(:,1));
T2 = unique(T);
A(T2) = A(T2) + T1(T2);
|