Turn a matrix into a function

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. 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