quick calculation in one second

1 view (last 30 days)
Hao Zhang
Hao Zhang on 13 Mar 2016
Commented: Walter Roberson on 14 Mar 2016
Hello, if there is a sequence x(i) (more than 1 million number points), how to optimize the algorithm and calculate the equation below in about one second? Thanks!
N = length(x);
J=[1 2 4 10 20 40 100 200 400 1000 2000 4000 10000 20000 40000 100000 200000 400000 1000000];
for h=1:1:19
q = J(h);
if (q<=N/3)
p(1:1:N-3*q+1)=0;
for j=1:1:(N-3*q+1)
p(j)=sum(x(j+2*q:1:j+3*q-1))-2*sum(x(j+q:1:j+2*q-1))+sum(x(j:1:j+q-1));
end
time(h)=q;
Stability(h) = sqrt(sum(p.*p)/(6*q*q*(N-3*q+1)));
end
end

Answers (1)

Walter Roberson
Walter Roberson on 14 Mar 2016
Florian was able to suggest a method there that took about 11 seconds in a parallel environment. Are you running a parallel environment?
Do you have a reason to expect that your formula can be calculated in about 1 second on whatever machine you are using?
  2 Comments
Hao Zhang
Hao Zhang on 14 Mar 2016
Yes, even it is in a parallel environment, it does not work as we expected. indeed, it can be done within one second. Calculation in a vector or matrix form , not using for loop, may be a possible way, i think.
Walter Roberson
Walter Roberson on 14 Mar 2016
You will not be able to evaluate that within 1 second by executing that formula in a straight-forward way. Each term is summed multiple times, with the overall number of summations on the order of magnitude of 6*m^2

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!