Scalar, Vector, and Matrix
A scalar is just a fancy word for a number; it is used to distinguish numbers from vectors or matrices.
Rows and Columns
You create a row matrix consisting of the numbers 4 5 6 7 by entering the numbers inside []-brackets and separating the numbers by a comma or a blank.
>>> r1=[4, 5, 6, 7] r1 = 4 5 6 7 >>> r2=[4 5 6 7] r2 = 4 5 6 7
You create a column matrix consisting of the numbers 0.1 0.2 0.3 by entering the numbers inside []-brackets and separating the numbers by a semicolon.
>>> c1=[0.1; 0.2; 0.3] c1 = 0.10000 0.20000 0.30000 >>> c2=[0.4; 0.5] c2 = 0.40000 0.50000
Row and column matrices are sometimes called vectors.
You can combine row matrices or column matrices
>>> r=[r1, r2]
r =
4 5 6 4 5 6
>>> c=[c1; c2]
c =
0.10000
0.20000
0.30000
0.40000
0.50000
>>> try_a_mix=[r1, c1]
>>>error: number of rows must match (3 != 1) near line 43, column 16
The first number in a vector has index 1. You can get the number at a specified index.
>>> r(2)
ans = 5
>>> c(5)
ans = 0.50000
>>> r(7)
error: A(I): Index exceeds matrix dimension.
You can use the same notation that creates numbers in for-statements for creating vectors.
>>> r3=3:7 r3 = 3 4 5 6 7 >>> r4=1:3:10 r4 = 1 4 7 10
You can use the same notation for extracting a vector from a vector.
>>> format bank; >>> r5=0.1:0.1:1 r5 = 0.10 0.20 0.30 0.40 0.50 0.60 0.70 0.80 0.90 1.00 >>> subvector1=r5(2:4) subvector1 = 0.20 0.30 0.40 >>> subvector2=r5(1:2:10) subvector2 = 0.10 0.30 0.50 0.70 0.90
You can turn a row into a column or the other way around by doing a transpose using the operator '.
>>> r=[1, 2, 3] r = 1.00 2.00 3.00 >>> c=r' c = 1.00 2.00 3.00
Arithmetic with matrices and scalars
You can use the arithmetic operators +
, -
, *
and /
on a matrix and a scalar. The operation is applied to each
element of the matrix.
>>> r=[4, 5, 6] r = 4.00 5.00 6.00 >>> r1=r*6 r1 = 24.00 30.00 36.00 >>> r2=(r1-20)/2 r2 = 2.00 5.00 8.00 >>> c=[0.5; 3.5] c = 0.50 3.50 >>> c1=c+0.5 c1 = 1.00 4.00
Functions of matrices
You can apply functions on matrices, the function is then applied to each element of the matrix.
>>> c=[4;16] c = 4.00 16.00 >>> sqrt(c) ans = 2.00 4.00
General matrices
A general matrix consists of n rows and m columns.
Try these commands:
m=[1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15] m+2 (m-3)*2 size(m) a=m(2,4) m>4
Exercise
Try the commands ones(3), zeros(4), eye(5)
!
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