How to increase the efficiency of this MATLAB program by using vectorization techniques
Show older comments
I have a MATLAB code snippet as below. For now I have tried two versions of implementation to apply a function standard_deviation_distance to each element in an array. I wonder if there are any potential improvements on the code efficiency/performance in terms of the running time optimization (i.e., I hope the code can be run as fast as possible). Any suggestions will be greatly appreciated!
P.S. Please see the file nodetraffic.mat in the attached
load nodetraffic.mat;
% Method I
tic
count1 = 0;
for i = 1 : length(nodetraffic)
if (standard_deviation_distance(nodetraffic, nodetraffic(i)) > 6)
count1 = count1 + 1;
end
end
count1
toc
%% Method II
tic
A = arrayfun(@(x) standard_deviation_distance(nodetraffic, x), nodetraffic);
count2 = length(A(A > 6))
toc
function dist = standard_deviation_distance(v, x)
standard_deviation = std(v);
dist = (x - mean(v)) / standard_deviation;
end
Accepted Answer
More Answers (0)
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!