An interactive introduction to MATLAB

(Jeff_L) #1

10 basic concepts


12 35 32
13 29 41
14 >> a.*b
15 ans =
16 8 21
17 45 6
18 >> c = [1 2 3 4]
19 c =
20 1 2 3 4
21 >> a*c
22 ??? Error using ==> mtimes
23 Inner matrix dimensions must agree.
24 >> a.*c
25 ??? Error using ==> mtimes
26 Matrix dimensions must agree.

Comments:


  • The dot operator signifies an element-by-element operation. The dot
    can be used for multiplication.*, division./, or exponentiation.^of
    elements of vectors that are the same size. Omitting the dot before an
    arithmetic operator meansMATLABperforms the matrix version of the
    operation.

  • On Line 21 we tried to perform a matrix multiplication of a 2×2 matrix
    with a 1×4 matrix. This results in an error because you can only multiply
    two matrices if the number of columns in the first equals the number of
    rows in the second.

  • On Line 24 we get a similar error if we try to perform an element-by-
    element multiplication, as this does not make any sense for matrices of
    different sizes.


Table 2: Element-by-element arithmetic operations
command description
.* Element-by-element multiplication
./ Element-by-element division
.^ Element-by-element exponentiation

The dot operator


(http://www.eng.ed.ac.uk/teaching/courses/matlab/unit01/dot-
operator.shtml)
Free download pdf