How can I speed up an exponential function?

2 ビュー (過去 30 日間)
Hans Wurst
Hans Wurst 2020 年 5 月 20 日
回答済み: James Tursa 2020 年 5 月 21 日
I am trying to get the (element-wise) exponential of a Matrix but I don't need most of the results. How can I use this to optimize my code. My attempts:
% Speedtest exponential
m=1000;
n=2000;
test1=rand(m,n);
tic
result=10.^test1;
toc
tic
test1(test1>0.01)=1;
result=10.^test1;
toc
tic
result=zeros(m,n);
for it1=1:m
for it2=1:n
if test1(it1,it2) > 0.01
result(it1,it2)=10^test1(it1,it2);
end
end
end
toc
I'm getting the following results:
Elapsed time is 0.095385 seconds.
Elapsed time is 0.021221 seconds.
Elapsed time is 0.167990 seconds.
Any way to do this more efficiently?

採用された回答

James Tursa
James Tursa 2020 年 5 月 21 日
I'm not sure what the issue is since you seem to already know about logical indexing. E.g.,
test1 = your data
x = test1 > 0.01; % your condition
result = zeros(size(test1)); % pre-allocate all of the spots
result(x) = 10.^test1(x); % only calculate the spots you are interested in

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by