Skip to content

Instantly share code, notes, and snippets.

@HajimeKawahara
Last active January 4, 2016 14:55
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 HajimeKawahara/4c182b7c2fe16cfaad05 to your computer and use it in GitHub Desktop.
Save HajimeKawahara/4c182b7c2fe16cfaad05 to your computer and use it in GitHub Desktop.
0 based and 1 based indexings
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"## conversion 0-indexing to Julia's one"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"getj (generic function with 1 method)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function getj(i,N)\n",
" j=round(Int64,rem(N+i,N)+1)\n",
" return j\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"### 0-index : [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f=[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4];"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-5"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f[1] ### wrong, should give 0 "
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 index=-5 -> corresponding 1-index=6\n",
"0 index=-4 -> corresponding 1-index=7\n",
"0 index=-3 -> corresponding 1-index=8\n",
"0 index=-2 -> corresponding 1-index=9\n",
"0 index=-1 -> corresponding 1-index=10\n",
"0 index=0 -> corresponding 1-index=1\n",
"0 index=1 -> corresponding 1-index=2\n",
"0 index=2 -> corresponding 1-index=3\n",
"0 index=3 -> corresponding 1-index=4\n",
"0 index=4 -> corresponding 1-index=5\n"
]
}
],
"source": [
"for i=-5:4\n",
" println(\"0 index=\",i,\" -> corresponding 1-index=\",getj(i,length(f)))\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.4.2",
"language": "julia",
"name": "julia-0.4"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment