Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Again, we pick MATLAB's brain on vectorization:

Wiki Markup*Help > Index (tab) > Vectorization \[1\]*Home (tab) > Documentation (icon) > Search: Vectorization > Vectorization (with book icon)

Go through the example. We'll vectorize our program by replacing loops with array operations. Since the syntax for array operations very closely resembles that for corresponding scalar operations, let's create our program starting from beam.m rather than loopy beam2.m.

Select beam.m in the Editor by clicking on its name at the bottom.

Editor (tab) > File Save > Save As

Enter beam3.m for filename. Confirm that you are editing beam3.m.

...

Note that this is achieved by preceding the ^ operator with the period character (.). The period character can be used with other arithmetic operators such as * and /. Chew over this as it is a subtle but powerful feature. For more info, see

Help > Search Home (tab) > Documentation (icon) > Search for: ) elementwise > Arithmetic Operatorarithmetic operators > Arithmetic Operators + - * / \ ^ '

So in the I statement, we replace ri^4 with ri.^4

...