How can I speed up an exponential function?

1 vue (au cours des 30 derniers jours)
Hans Wurst
Hans Wurst le 20 Mai 2020
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?

Réponse acceptée

James Tursa
James Tursa le 21 Mai 2020
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

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by