How can I speed up an exponential function?
조회 수: 6 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!