how to increase the run time of the matrix ?
1 ビュー (過去 30 日間)
I am runnng a code for finding the shorter path , i am able to do it easily for matrix of 37*37 but as I increase its value it start to take lot of time.I tried to preallocate few values in it but not able to do it easily.
I am attaching 2 files for the reference. less_time.m is of 37*37 matrix and more_time.m is of 148*148 matrix.
kindly guide me how to preallocate the values in it so that I can increase the time of the operations
回答 (2 件)
Jan 2021 年 3 月 25 日
編集済み: Jan 2021 年 3 月 25 日
Some standard ideas for improving the runtime:
- sqrt(X) is faster than X^0.5
- The JIT acceleration is more powerful, if you write one command per line. Avoid constructs like this: "mink=k;minl=m;minkl=PLkm;"
- Logical indexing is faster than numerical indexing.
PLKPLK = PLK(PLK ~= 0);
- This works, but the multiplication by 1 is confusing only, most of all in the combination with a lowercase L:
D = zeros(l*1,l*1);
D = zeros(l, l); % But I avoid "l" consequently to reduce confusions
- If you need 1 element only, do not waste time with searching all elements:
Select = find(Pcum>=rand, 1);
- In line 110 I assume you want a vector, not a matrix: "PP=zeros(Len_LJD);" Use PP=zeros(Len_LJD, 1) instead.
- Then you can replace:
PP=PP/sumpp;%Build probability distribution
PP = Tau(W,LJD).^Alpha .* Eta(LJD).^Beta;
PP = PP / sum(PP);
Pcum = cumsum(PP);
Select = find(Pcum >= rand, 1);
to_visit = LJD(Select);