How to effectively run loops and save time in computation? I have a matrix of size 'm' and run five loops from 1 to m.. How to optimize the code and save time of calculation for the code below

2 Ansichten (letzte 30 Tage)
Can you help in optimizing the code below so that the execution time can be reduced? For a very large matrix of size 500x500, the computation time is huge. How to reduce it?
Since floor function is used here, is there any way to effectively compute the below code with both Time and Space efficiency..
function [PS] = PS_sparse(H)
[m,n]=size(H);
Ohmega=H*H';
for k=1:m
for i=1:m
for j=1:m
if j~=i
x(j)=abs(Ohmega(i,k)+Ohmega(j,k));
end
end
mask=x~=0; % mark the non-zero elements
x=x(mask); % keep the non-zero elements
x0=sort(x,'ascend'); % order the x so that lomed can be calculated
y(i)=x0(floor((length(x0)+1)/2)); % calculate the low median
end
y0=sort(y,'ascend'); % order the x so that lomed can be calculated
sm(k)=1.1926*y0(floor((length(y0)+1)/2));% calculate the low median
end
for k=1:m
for i=1:m
aux(i)=abs(Ohmega(k,i))/sm(i);
end
PS(k,1)=max(aux);
end
The above code was taken from https://in.mathworks.com/matlabcentral/fileexchange/60838-matlab-code-of-robust-gm-estimator-for-power-system-state-estimation-using-projection-statistics?focused=7509217&tab=function
  1 Kommentar
Stephen23
Stephen23 am 4 Mär. 2018
Bearbeitet: Stephen23 am 4 Mär. 2018
"Can you help in optimizing the code below so that the execution time can be reduced? ... How to reduce it?"
Follow the guidelines in the MATLAB documentation:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 4 Mär. 2018
Bearbeitet: dpb am 4 Mär. 2018
Innermost loop
for j=1:m
if j~=i
x(j)=abs(Ohmega(i,k)+Ohmega(j,k));
end
has "issues" -- first off, x isn't preallocated nor reinitialized so after the first loop there will be remnants of the previous left over.
In the loop on j, i, k are invariant so there's really no need for the loop at all...it can be replaced with
x=abs(Ohmega(i,k)+Ohmega(:,k)); % build the vector
x(i)=0; % fixup the diagonal location
There's superfluous code following...
...
mask=x~=0; % mark the non-zero elements
x=x(mask); % keep the non-zero elements
...
can be just
x=x(~=0); % don't need the temporary mask array for anything
Then
...
x0=sort(x,'ascend'); % order the x so that lomed can be calculated
y(i)=x0(floor((length(x0)+1)/2)); % calculate the low median
...
can use builtin median that should be faster. It returns MEAN(two center elements) if m is even; if algorithm must use the actual data point within the vector then augment the x vector with an artificial zero; possibly just eliminating the previous step would suffice in that case.
y(i)=median(x);
Similarly for y0 on the way to sm
None of the LHS variables are preallocated; similar logic can also be applied to the last loop as well.
That's a fast-brush look-see...

Weitere Antworten (0)

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by