A matrix (or a number) is already a linear function via the multiplication operation
A(x) = Ax. Yet, it's
also possible to turn the matrix indexing expressions into functions of the
index.
- If A is a vector (a
or
matrix), then f = A[.] creates a function
of one variable such that f(x) = A[x].
- If A is matrix, then f = A[., .] creates a function
of two variables such that f(x, y) = A[x, y].
- If A is matrix, then f = A[., :] creates a function
of one variable such that f(x) = A[x, :], which gives the x-th
row of A.
- If A is matrix, then f = A[:, .] creates a function
of one variable such that f(x) = A[:, .], which gives the x-th
column of A.
- If A is matrix, and k is a constant (a scalar or vector that
is a valid column index for A), then f = A[., k] creates a function
of one variable such that f(x) = A[x, k].
- If A is matrix, and k is a constant (a scalar or vector that
is a valid row index for A), then f = A[k, .] creates a function
of one variable such that f(x) = A[k, x].
For example
>> s = floor(rand(5) * 10)
5
3
0
7
2
>> fs = s[.];
>> fs(1)
5
>> fs(3)
0
Another example
>> s = floor(rand(5, 5) * 10)
7 0 5 8 3
9 0 3 7 8
1 6 6 7 9
2 5 5 3 2
1 2 5 5 0
>> fr = s[., :];
>> fr(1)
7 0 5 8 3
>> fr(3)
1 6 6 7 9
>> fc = s[:, .];
>> fc(1)
7
9
1
2
1
>> fc(3)
5
3
6
5
5
Sometimes a structured set of data is stored most efficiently in a vector or
matrix. However a client function that needs to use the data may expect a function
that returns one data item given an index.
In such a situation we can store the data in a matrix A, and pass A[.]
whenever the client function is being called.
oz
2009-12-22