Skip to content

Instantly share code, notes, and snippets.

@njvack
Created March 13, 2015 14:11
Show Gist options
  • Save njvack/5efb67bb898cd942fd52 to your computer and use it in GitHub Desktop.
Save njvack/5efb67bb898cd942fd52 to your computer and use it in GitHub Desktop.
A Slicing Notebook
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:d5c51bcf5cc92704d68f89b0ed049c4e65540baa2f38f28aab21f1b3467366bd"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = np.linspace(0,10,50)\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"array([ 0. , 0.20408163, 0.40816327, 0.6122449 ,\n",
" 0.81632653, 1.02040816, 1.2244898 , 1.42857143,\n",
" 1.63265306, 1.83673469, 2.04081633, 2.24489796,\n",
" 2.44897959, 2.65306122, 2.85714286, 3.06122449,\n",
" 3.26530612, 3.46938776, 3.67346939, 3.87755102,\n",
" 4.08163265, 4.28571429, 4.48979592, 4.69387755,\n",
" 4.89795918, 5.10204082, 5.30612245, 5.51020408,\n",
" 5.71428571, 5.91836735, 6.12244898, 6.32653061,\n",
" 6.53061224, 6.73469388, 6.93877551, 7.14285714,\n",
" 7.34693878, 7.55102041, 7.75510204, 7.95918367,\n",
" 8.16326531, 8.36734694, 8.57142857, 8.7755102 ,\n",
" 8.97959184, 9.18367347, 9.3877551 , 9.59183673,\n",
" 9.79591837, 10. ])"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x[0:5]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653])"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"starts = np.array([0,10,21])\n",
"length = 5\n",
"ends = starts + length\n",
"pairs = np.vstack([starts, ends]).T\n",
"ranges = np.apply_along_axis(lambda p: np.arange(*p), 1, pairs)\n",
"ranges"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"array([[ 0, 1, 2, 3, 4],\n",
" [10, 11, 12, 13, 14],\n",
" [21, 22, 23, 24, 25]])"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x[ranges]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"text": [
"array([[ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653],\n",
" [ 2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286],\n",
" [ 4.28571429, 4.48979592, 4.69387755, 4.89795918, 5.10204082]])"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment