Skip to content

Instantly share code, notes, and snippets.

@geoffrasb
Created November 14, 2012 22:21
Show Gist options
  • Save geoffrasb/4075271 to your computer and use it in GitHub Desktop.
Save geoffrasb/4075271 to your computer and use it in GitHub Desktop.
matlab: matrix construction
expand(vec,dims)
# split vec into layers
ex:expand([1;2],[]) -> [1;2]
expand([1;2],[2]) -> [1 2;
1 2]
expand([1;2],[2 3]) ->
1 1 1 2 2 2
1 1 1 2 2 2 (a 2*3*2 matrix)
expand([1;2;3],[1 2]) ->
1 1 2 2 3 3 (a 1*2*3 matrix)
rep(mat,dims)
# induce on dims(a dimension range) with recursively calling repmat
case dims of
[] -> mat
otherwise -> rep(repmat(mat,car(dims)),cdr(dims))
where car([a b]) = a
cdr([a b]) = [b]
ex:
rep([0;1], [2 3])
= rep([0 0 , [3])
1 1]
= 0 0 0 0
1 1 1 1 (a 2*2*2 matrix)
zipWith f mats
# mats is a list of matrix with the same size
# f is a function with arity = length(mats)
ex: zipWith f [[1 2] [3 4]]
= [f(1,3) f(2,4)]
#----------------------------------------------
for Vectors V = {Vi | i=1:n}
and L = {Li=length(Vi) | i=1:n}
and f with arity = n
a matrix construction that interweaving V can be written as:
zipWith f [ rep(expand(Vi, [Li for i=1:i-1]), [i+1:n]) for i=1:n ]
next task: find corresponding functions in matlab...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment