Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

151 lines (88 sloc) 4.312 kb

METHLABS!

bin/methlabs

This allows matlab scripts to be executable given they start with a shebang. For example:

#!/usr/bin/env methlabs
fid = fopen("hiworld.test", 'a');
fclose(fid);

can be ran like this:

./hiworld.m

and the file "hiworld.test" will be created!

modules/*

Some various m-files for doing all sorts of things! A lot of them were influenced by Haskell. At least one was influenced by python, and another by a certain unix util. The rest? Things I thought would have high utility, of course.

Use:

Either copy the necessary m-files to your directory or use addpath() to get MATLAB to look for them. For example:

addpath(genpath('/home/josh/methlabs/modules/'));

Some notes:

You may be wondering, 'where's map?' Well, the answer is, MATLAB already has map! It just has an obnoxious name! In particular:

  • arrayfun(f,x): map for matrices/arrays
  • cellfun(f,x): map for cells

modules/compositions:

Functions in here make new functions out of other functions.

  • compose(f,g,h...n): composes n functions together in the form of f(g(h(...n(x)...))).

modules/submatrices:

These functions return certain submatrices:

  • ahead(A): First column of A
  • atail(A): All but the first column of A
  • ainit(A): All but the last column of A
  • alast(A): the last column of A

modules/folds:

These functions are basically variations of the idea of lisp's "reduce" and haskell's "foldl" and "foldr." However, these functions also fold up and down. All of the folds require "ainit.m" and "alast.m" from modules/submatrices, and afoldr(), afoldu() and afoldd() require "areverse.m" from modules/transforms.

All four functions follow this format:

afold*(fxn,x0,matrix);

where * refers to the direction (left, right, up or down), fxn refers to the function used in the fold, x0 refers to the initial value, and matrix is the matrix used.

For example:

afoldl(@(x,y) x+y,[0;0] [1,2,3,4,5;5,4,3,2,1]);

means, in other words,

((([0;0] + [1;5]) + [2;4])+[3;3])+[4;2])+[5;1] == [15;15]

foldr would start from the other side (and in the case of @(x,y) x+y would achieve the same result), while foldu would run row-wise from top to bottom:

octave:3> afoldd(@(x,y) x+y, [0,0,0,0,0], [1,2,3,4,5;5,4,3,2,1])
ans =

   6   6   6   6   6

modules/transforms:

  • areverse() reverses a matrix. For example:

    octave:5> areverse([5,4,3,2,1])
    ans =
    
       1   2   3   4   5
    

areverse() requires "ainit.m" and "alast.m" from modules/submatrices.

  • flatten() takes an mxn matrix and returns a 1x(m*n) matrix. For example:

    octave:6> flatten([1,2,3;4,5,6;7,8,9])
    ans =
    
       1   2   3   4   5   6   7   8   9
    
  • str2cell() is an extra array-to-cell conversion function, which takes an mxn array of characters and converts it to a mx1 cell:

    octave:7> a=['foo';'bar']
    a =
    
    foo
    bar
    
    octave:8> str2cell(a)
    ans =
    
    {
      [1,1] = foo
      [2,1] = bar
    }
    

modules/extra

  • replicate.m is inspired by haskell's replicate, and, when called, returns a cell of size 1xn each with a value of k:

    octave:2> replicate(5,3)
    ans =
    
    {
     [1,1] =  3
     [1,2] =  3
     [1,3] =  3
     [1,4] =  3
     [1,5] =  3
    }
    

replicate's default behavior is to return a cell, but it can also return a matrix:

octave:3> replicate(5,3,'a')
ans =

   3   3   3   3   3

Note that, in this simple case, it would make more sense to have typed * 3*ones(5,1) *.

  • elemsof(A) returns the number of elements in A. This is exactly equivalent to size(A,1)*size(A,2).

  • strjoin.m is inspired by python's string join method, and works similarly:

    octave:10> strjoin(',','foo')
    ans = f,o,o
    

Simplest yet, right?

BUT WAIT THERE'S MORE

  • touch.m does pretty much the same thing the unix utility touch does:

    Update the access and modification times of each FILE to the current time.

    A FILE argument that does not exist is created empty.

touch.m is much simpler, and only takes the current file as an argument. If you want the rest of touch's functionality, use system('touch $file');.

Jump to Line
Something went wrong with that request. Please try again.