How to create array with unequal spacing?
이전 댓글 표시
I want to create an array with unequal spacing. I have h = 1 1/2 1/4 1/8 ..... 2^-60.
Every second entry in this array is 1/2 of the last one. I only know, how to make arrays with equal spacing.
a = 1:5:40
But how do I create array that gets halved with every entry?
댓글 수: 5
h = pow2(0:-1:-60)
Walter Roberson
2018년 2월 21일
timeit() disagrees:
>> timeit(@() pow2(0:-1:-60),0)
ans =
3.45591429508197e-06
>> timeit(@() 2.^-(0:60),0)
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
> In timeit (line 158)
ans =
0
And if you
>> tic;for K = 1 : 1000; h = 2.^-(0:60); end; toc
Elapsed time is 0.000007 seconds.
-- not the first time, at least not from the command line, but after a few times repeating the same line, the JIT fully kicks in.
By way of contrast,
>> tic;for K = 1 : 1000; h = pow2(0:-1:-60); end; toc
Elapsed time is 0.004652 seconds.
was the best I could do.
Ahmad Hasnain
2018년 2월 22일
Stephen23
2018년 2월 22일
@Walter Roberson: Interesting. So what is the usecase for pow2(n) then?
Walter Roberson
2018년 2월 22일
In 2.^-(0:60) the JIT is detecting that the expression is constant and optimizes it, at least in the case of re-use. If you use B = 2; B.^-(0:60) or if you use V = -(0:60); 2.^B then JIT optimization is not as good and pow2 can have better performance than those.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!