possible to increase speed of power function applied to a vector?

7 ビュー (過去 30 日間)
ER2018
ER2018 2018 年 7 月 2 日
コメント済み: ER2018 2018 年 7 月 3 日
Is it possible to increase speed of power function applied to a vector?
N=16e3;
X=-2+4*rand(N,1);
Y=10.^X;
I tried
Y=power(10,X);
But of course it gives me the same speed. Any ideas?

回答 (2 件)

Jan
Jan 2018 年 7 月 2 日
No. power is expensive. There is no magic super power operator, which can do the same work in less time.
In some situations you can replace the power operation by accumulated multiplications:
a = rand;
X1 = a .^ (1:10);
X2 = cumprod(repmat(a, 1, 10));
But remember that this accumulates the rounding errors also, so for large powers it gets less accurate.
Another common case, where the power operation can be avoided:
a = 1.5 * 10^8 % Expensive power and multiplication
b = 1.5e8 % Cheap constant
Especially if this appear in loops or in functions to be integrated, the constant can save much time.
  1 件のコメント
ER2018
ER2018 2018 年 7 月 3 日
Thank you for the tips. Unfortunately there's no cheap constant I can use here, I really need the calculated values.
How would the solution with cumprod look in the example I provided?

サインインしてコメントする。


Yair Altman
Yair Altman 2018 年 7 月 3 日
In addition to what Jan said, perhaps your in your specific case it is possible to work at the exponent level, i.e. not Y=10^X but rather log10Y=X, and then continue processing using log10Y rather than with Y. Depending on what you do with the data later in your code, it might possibly be faster since it avoids the initial power operation.
  1 件のコメント
ER2018
ER2018 2018 年 7 月 3 日
Yes, I do that when possible, but finally I do need to calculate the actual values 10.^X to use in the rest of the code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by