How can I speed up an exponential function?

6 visualizaciones (últimos 30 días)
Hans Wurst
Hans Wurst el 20 de Mayo de 2020
Respondida: James Tursa el 21 de Mayo de 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?

Respuesta aceptada

James Tursa
James Tursa el 21 de Mayo de 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

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by