Skip to content

Instantly share code, notes, and snippets.

@Mic92
Last active June 10, 2016 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/4cd8b87c86b83c25ab028e0652092664 to your computer and use it in GitHub Desktop.
Save Mic92/4cd8b87c86b83c25ab028e0652092664 to your computer and use it in GitHub Desktop.
Port of embed to python
import numpy as np
def embed(x,y, dimension):
"""
embed 2 vectors
>>> embed([1,2,3], [4,5,6], 2)
array([[ 2., 5., 1., 4.],
[ 3., 6., 2., 5.]])
>>> embed([1,2,3], [4,5,6], 3)
array([[ 3., 6., 2., 5., 1., 4.]])
>>> embed([1,2,3,4,5], [4,5,6,7,8], 2)
array([[ 2., 5., 1., 4.],
[ 3., 6., 2., 5.],
[ 4., 7., 3., 6.],
[ 5., 8., 4., 7.]])
"""
assert len(x) == len(y), "both vectors x and y should have same length"
n = len(x)
m = 2
matrix = np.zeros((n - dimension + 1, dimension * 2))
for row in range(matrix.shape[0]):
for col in range(dimension):
matrix[row, (col * 2)] = x[row + (dimension - col) - 1]
matrix[row, (col * 2) + 1] = y[row + (dimension - col) - 1]
return matrix
if __name__ == '__main__':
import sys, doctest
sys.exit(doctest.testmod()[0])
> embed(c(1, 2, 3), 2)
[,1] [,2]
[1,] 2 1
[2,] 3 2
> embed(c(1, 2, 3, 4), 2)
[,1] [,2]
[1,] 2 1
[2,] 3 2
[3,] 4 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment