A1: Add, subtract, multiply, divide
A1: Add, subtract, multiply, divide
Most of these commands cast Matlab in the role of a fancy calculator. Thus, you can type:
>> 8*7
to find ‘the product of eight and seven’, or:
>> 3+1
for ‘the sum of three and one’, or:
>> 2^9
to compute ‘two raised to the ninth power’.
For the multiplication-based operations, *, ^, and /, there is an additional complication when you are multiplying lists of numbers. Suppose you assign the list of numbers [1, 3, 5] to the variable x:
>> x=[1 3 5];
and you want to multiply this list by the list y=[2 4 6]. You place a ‘.’ next to the multiplication symbol to indicate that you want the products [1*2, 2*4, 1*6] rather than the matrix product x*y. The same holds true for exponentiation and division, and in most cases we will want .*, .^, and ./ instead of the matrix-based operations. To experience the difference, try the following:
>> y=[2 4 6];
>> x.*y
>> x*y'
>> x*y
>> y.^x
>> y./x
>> y/x
First, notice that one of these produces an error. This is because the matrix multiplication of a 1x3 and a 3x1 matrix (y’ is the transpose of y) can be done (and produces a result with dimension 1x1), but a matrix multiply of a 1x3 and a 1x3 matrix cannot be done.