next up previous
Next: Plots of functions Up: Waves Previous: Comparison to Fortran and

Array calculations

When you perform mathematical operations on arrays or matrices in MATLAB you need to use syntax which is slightly different than when you are using MATLAB as a calculator.

For example if we define b=[10., 20., 30.] and a=[1., 2., 3.] and then the correct way to add these arrays in MATLAB is b+a but the correct way to multiply them element by element is b.*a, which produces [10., 40., 90.] Note the extra period before the multiply symbol *. If you say b*a, you get an error in MATLAB because the program thinks you want to multiply a 1 by 3 matrix by a 1 by 3 matrix, which is not possible.

There is a similar divide symbol ./ and an exponent symbol .^ . For example, a.^ 2 produces [ 1., 4., 9.].



VICTOR W SPARROW
Tue Feb 25 21:24:33 EST 1997