Element by Element
You can perform element by element operations on two matrices having the same dimension, i.e. having the same number of rows and columns respectively. When performing an element by element operation the result is a new matrix having the same dimension as the two operands.
When doing an element by element addition, the element on place (row,
col) in the resulting matrix will be the sum of the two elements at (row,
col) in the operand matrices.
The regular arithmetic operators will become element-by-element operators if you place a dot in front of them.
.+ .- .* ./ .^
>>> m1 m1 = 1 2 3 4 5 6 >>> m2 m2 = 0 1 1 2 2 0 >>> m1./m2 ans = Inf 2.0000 3.0000 2.0000 2.5000 Inf >>> m1.^m2 ans = 1 2 3 16 25 1
When applying a function on a matrix the function is applied to each element of the matrix
>>> m=[0 pi pi/2;-pi -pi/2 0] m = 0.00000 3.14159 1.57080 -3.14159 -1.57080 0.00000 >>> sin(m) ans = 0.00000 0.00000 1.00000 -0.00000 -1.00000 0.00000
Using element by element operations you can combine comparisons and arithmetic.
>>> m=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15] m = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 >>> m1=m>4 m1 = 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 >>> m2=m1.*m m2 = 0 0 0 0 5 6 7 8 9 10 11 12 13 14 15
Regular linear algebra
In regular mathematics, matrix addition and subtraction are defined to be element
by element operations. Since using the Octave operators without any dot means
"regular" usage, there is no difference between + and
.+, or between - and .-. When it comes
to multiplication, division and to-the-power-of, there is a difference between
"regular" usage and the element-by-element usage; hence do not use
these operators without a dot in front of them (unless you actually know linear
algebra).
further info:
by Malin Christersson under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Sweden License