This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/programming/code/logicalIndexingBenchmark.m

22 lines
417 B
Matlab

% create a vector with random numbers
x = rand(1000000, 1);
fprintf('Time needed to manually filter out elements smaller than 0.5 in a vector of length %i\n', length(x))
tic
results = [];
matches = 1;
for i = 1:length(x)
if x(i) < 0.5
results(matches) = x(i);
matches = matches + 1;
end
end
toc
fprintf('\nTime needed to do the same with logical indexing\n')
tic
results = x(x < 0.5);
toc