|
Department of Engineering |
|
|
Next: User Interface Controls
Up: Examples
Previous: Program-generated names and commands
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