Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: User Interface Controls Up: Examples Previous: Program-generated names and commands

Vectorised code

Code can be made shorter and faster by exploiting vectorisation - removing for loops.
% This creates a 10x10 matrix using the magic command, then finds
% the mean of each  column, ignoring any element less than 10.
% Note that no explicit loops are used.
array=magic(10)
keep = (array>=10);
colSums = sum(array .* keep);
counts = sum(keep); 
means = colSums ./ counts
For many more examples see Vectorisation Tricks



© Cambridge University Engineering Dept
Information provided by Tim Love
2006-07-24