Loop vectorization
| ID | STAT-25 |
| Title | Vectorize loops where feasible. |
| Priority | Strongly recommended |
| Severity level | 10 |
| Description | Instead of looping over the elements of arrays, apply operations to them in their entirety. |
| Rationale | Vectorized loops are faster, more robust, more readable, and more maintainable. |
Avoid:
dataResult = false(size(dataset));
for iElem = 1 : numel(dataset)
if dataset(iElem) < limit(iElem)
dataResult(iElem) = true;
else
dataResult(iElem) = false;
end
end
Instead use:
dataResult = dataset < limit;