Since learning Lisp writing
for loops in Matlab seems even less appetizing that the concerns of vectorization dictate it would already be. Higher order functions let you do some very nice things in a very small amount of space, and I've gradually integrated some of that power into my Matlab code. I've created a group of functional style programming functions which "do the right thing" in the context of Matlab's multidimensional matrices and cell arrays - as long as "the right thing" isn't "be very fast."
These functions are not meant for optimized code and are quite slow. What they are useful for is filtering lists of strings which represent data sets or doing more complicated operations on inhomogeneous inputs without writing for loops in preparation for performance critical code. They've made my life as a data analyst more fun and at least a little easier.
There are five functions:
map,
filt,
partial,
ix, and
ixc
Here are the documentation strings (link to a zip at the bottom).
% F=PARTIAL(F,ARG) given F and ARG returns F curried on ARG
%
% F=PARTIAL(F,ARG) partially evaluates F with the first argument ARG
% and returns a function of the remaining arguments.
%
% F=PARTIAL(F,ARG,POSN) partially evaluates F with the POSN placement
% and value arg and returns a function of the remaining arguments, in the
% same order.
% RES=MAP(F,MATRIX1,...,MATRIXN) Maps F of arity N onto MATRIX1 ... N
% When F is a function of N variables, it is applied to
% each set of things in MATRIXs in column major order.
%
% When all MATRIXs are numeric and the same shape, the result
% is a numeric MATRIX of the if F returns a single number. Otherwise
% the result is a CELL ARRAY of the same shape as the inputs.
%
% If the MATRIXES are different in size, the smallest one determines the
% output, which is returned as a matrix of that shape.
% [R1,R2,...,RN] = FILT(F,MATRIX1,MATRIX2,...,MATRIXN) filters MATRIXES by F
% R1,R2,...,RN are the values of MATRIX1 MATRIX2 ... MATRIXN where F is true.
% These are generally returned as flat arrays unless F is always true. Then
% this is the identity.
% RES = IX(MATRIX,INDS1,INDS2,...,INDS3) indexes MATRIX with INDS
% Identical to MATRIX(INDS1,INDS2,INDS3)
% Does not support END notation
% IXC is like IX except uses cell indexing.
Archive:
matlab_functional
Let me know if anyone uses them.
4 comments:
I'm interested in using your tools but I'd like to know what kind of license is attached to them.
Also, since you're a lisper and I presume you are accustomed to comfortable environments like Slime, I'd appreciate it if you could share some general tricks on making Matlab more lisper-friendly.
Thanks in advance :-)
I can't download your code from the link given http://neuro.physics.unc.edu/~toups/files/matlab_functional.zip
Could you please correct link so I download your files?
Thanks,
Nordlöw
Nordlow, the latest version is in my github repo for Matlab code. Check the functional directory.
https://github.com/VincentToups/matlab-utils
Post a Comment