{ "metadata": { "name": "matlab_vs_python" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Requirements\n", "------------\n", "\n", "This notebook requires oct2py and octave\n", "\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext octavemagic\n", "import numpy as np # by convention" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now type matlab (octave) commands into the notebook" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%octave\n", "a = rand(4,8);" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# We could this in python like this\n", "# notice the first difference: namespaces\n", "# The python functions are not stored in the general namespace \n", "# The rand function is stored in the numpy.random package.\n", "# This keeps the environment cleaner\n", "\n", "np.random.rand(4,8)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's get the octave variable into python\n", "%octave_pull a" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# These should be the same now\n", "print(a)\n", "%octave a" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Introspection\n", "--------------\n", "\n", "Ok now let's get to work\n", "Let's start with some simple introspection" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# shape and size\n", "print('rank'.center(20, '*'))\n", "%octave disp(ndims(a))\n", "print(a.ndim)\n", "print('type'.center(20, '*'))\n", "%octave disp(class(a))\n", "print(a.dtype)\n", "print('shape'.center(20, '*'))\n", "%octave disp(size(a))\n", "print(a.shape)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# Get a list of variables and check if a is in there.\n", "print('a' in dir())\n", "%octave who a" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# What can we do with a, only show the last 10\n", "dir(a)[-10:]\n", "# no equivalent in matlab" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing\n", "---------" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# get the first element\n", "%octave a(1)\n", "a[0,0]\n", "# Two differences here:\n", "# - indexing a matlab 2d array with 1 value gives 1 value\n", "# - indexing a python 2d array with 1 value gives 1 row\n", "\n", "# - python starts counting at 0\n", "# - matlab starts counting at 1\n", "# http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# get the first row\n", "% octave a(1,:)\n", "a[0,:]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [ "# get the first column\n", "%octave a(:,1)\n", "a[:,0]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": "*" } ], "metadata": {} } ] }