How to increase the efficiency of this MATLAB program by using vectorization techniques
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!