{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "From Matlab to Python\n", "=====================\n", "\n", "Important commands\n", "------------------\n", "\n", "### The for-loop\n", "\n", "Matlab:\n", "\n", "```octave\n", "for i = 1:10\n", " disp(i)\n", "end\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matlab requires the `end` key-word at the end of the block belonging to the for-loop.\n", "\n", "Python:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n" ] } ], "source": [ "for i in range(1,11):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python requires a colon (“:”) at the of the `for`-line. (This is important and often forgotten when you have programmed in Matlab before.) Python requires the commands to be executed within the for-loop to be indented.\n", "\n", "### The if-then statement\n", "\n", "Matlab:\n", "\n", "```octave\n", "if a==0\n", " disp('a is zero')\n", "elseif a<0\n", " disp('a is negative')\n", "elseif a==42\n", " disp('a is 42')\n", "else\n", " disp('a is positive')\n", "end\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matlab requires the `end` key-word at the very end of the block belonging to the for-loop.\n", "\n", "Python:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a is negative\n" ] } ], "source": [ "a = -5\n", "\n", "if a==0:\n", " print('a is zero')\n", "elif a<0:\n", " print('a is negative')\n", "elif a==42:\n", " print('a is 42')\n", "else:\n", " print('a is positive')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python requires a colon (“:”) after every condition (i.e. at the of the lines starting with `if`, `elif`, `else`. Python requires the commands to be executed within each part of the if-then-else statement to be indented.\n", "\n", "### Indexing\n", "\n", "Matlab’s indexing of matrices and vectors starts a 1 (similar to Fortran), whereas Python’s indexing starts at 0 (similar to C).\n", "\n", "### Matrices\n", "\n", "In Matlab, every object is a matrix. In Python, there is a specialised extension library called `numpy` (see Sec. \\[cha:numer-pyth-numpy\\]) which provides the `array` object which in turns provides the corresponding functionality. Similar to Matlab, the `numpy` object is actually based on binary libraries and execution there very fast.\n", "\n", "There is a dedicated introduction to numpy for Matlab users available at ." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.4" } }, "nbformat": 4, "nbformat_minor": 1 }