Matlab to Python – some code examples

Two years back, I was converting a matlab script to python, here are some of the errors that I encountered during the conversion. Found them documented in that converted script, posting them here for wider audience.


Matlab to Python

1. () to []


SyntaxError: can't assign to function call


2. 1 to 0


IndexError: index (2) out of range (0<=index<2) in dimension 1


3. zeros to zeros


In Python


4. array to array

D=G*[1 0; 0 1];

in python

D_ps=G* np.array([[1.0,0.0],[0.0,1.0]])

5. % to #

% is comments in matlab


# is comments in python

6. For to for

for i=1:nnel


for i in range(nnel):

7. Matlab find to python find

L1 = find(coordinates(:,2)==min(coordinates(:,2)))

in python

l1 = np.where(coords[:,1]==np.min(coords[:,1])


Here’s a text from the book intution pumps by Daniel Dennett

You can shingle a roof, paint a house, or fix a chimney with the help of just a ladder, moving it and climbing, moving it and climbing, getting access to only a small part of the job at a time, but it’s often a lot easier in the end to take the time at the beginning to erect some sturdy staging that will allow you to move swiftly and safely around the whole project

from: “Intuition Pumps And Other Tools for Thinking” by Daniel C. Dennett

An apt word for the building blocks work I am currently involved with. 

Living With The Black Swan


From about 2000 years, in many European languages, a black swan was a metaphor for something that was clearly impossible. And then black swans were found in Australia. So a black swan became a metaphor for a completely unexpected event actually occurs, one we had not imagined was impossible. Black swans appear regularly – Skype, iPhone, the Cloud…. If a black swan landed in your marketplace, would you recognize it? Most companies don’t. It’s no coincidence that the average age of companies – big companies – is falling fast, at the same time that black swan events are increasing.

You never see black swans coming – you have to be ready to respond when they arrive. 

This talk is about the kind of thinking and organizational structure that can help you live successfully with the black swans. It is about how to build an innovative, responsive, enduring organization

Goes well with this post on antifragile.

Pdf With Matplotlib

I thought everyone knew about but i was surprised this this little feature of matplotlib is not that known as widely as I assumed.

We all know we can save a plot from matplotlib to pdf but there other little feature hiding in the backends where we can write out a multiple page pdf

Here’s a simple code to write out multiple page pdf using matplotlib.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages





So easy. Instantiate pdfpages, Open the pdf file, save figures. Works from version 0.99 of matplotlib.
With little use of plt.text, plt.annotate, can be used to produce quick pdf reports. In fact,I have pandas and matplotlib workflow to pump out full blown management report on continuous improvement initiative.
Here’s a simple full example of creating a simple pdf report using the above feature

Continue reading

Reading list and Lollapalooza effect

Never thought giving up motorbike ride will be so fruitful. Recent location change has put plenty of alone walking time to my office and this had an unexpected effect on my book reading.Here’s the list of the books that I have read since my last post on books. 

  1. Purple cow by Seth Godin
  2. Being Mortal Atul Gwande
  3. Seeking wisdom from Darwin to Charlie Munger by Peter
  4. The Warren Buffet Portfolio by Robert G
  5. The Myth of innovation by Scott Berken 
  6. Permission marketing by Seth Godin
  7. The little book of big dividend investing by 
  8. The road less travel by
  9. The Wright Brothers By David McCullough
  10. How to drive a tank and other everyday tips for the modern gentleman – Frank Coles
  11. The Wandering Mind by Michael C Cornallis
  12. Rise of robots: technology and the threat of a jobless future by Martin Ford
  13. All marketers are liars by Seth Godin
  14. Irrationally yours: on missing stocks, pickup lines and other existential puzzles by Dan Ariely
  15. The road to character by David Brooks
  16. Surely you are joking Mr. Feynman by Ralph Leighton
  17. The selfish gene by Richard Dawkins 
  18. Misbehaving: the making of behavioral economics by Richard H Thaler

Am surprised at the length of the list. I guess this is what Charlie Munger calls a lollapalooza effect. Combination of factors leading to an outstanding results. No bike, long walks to office and plenty of alone time.  

My top three books of the lot are

  1. Seeking wisdom
  2. Rise of robots
  3. Surely you are joking Mr. Feynman

Which one have you read?

Exploring MFEM

mfem c++ fem library

With little time I have after office and playing with kids , I am devoting some to this MFEM library.

Really good piece of software. Exploration continues.

So What is MFEM?

No, it’s not Ministry of Finance and Economics Management, it’s …….

MFEM is a lightweight, general, scalable C++ library for finite element methods and available at

The goal of MFEM is to enable research and development of scalable
finite element discretization and solver algorithms through general
finite element abstractions, accurate and flexible visualization, and
tight integration with the hypre linear solvers library. Its features
– 2D and 3D, arbitrary high-order H1, H(curl), H(div), L2 and NURBS
– Parallel version scalable to hundreds of thousands of MPI cores.
– Conforming or nonconforming adaptive mesh refinement (AMR),
including anisotropic refinement.
– Galerkin, mixed, isogeometric, DG and DPG discretizations.
– Support for triangular, quadrilateral, tetrahedral and hexahedral
elements with curved boundaries.
– Lightweight interactive OpenGL visualization with GLVis,

An interactive documentation of MFEM’s serial and parallel example
codes can be found here
MFEM is freely available under LGPL 2.1.


How a driverless car sees the road- See Machine Learning in Action

Haven’t seen anything so exciting for a long time. Exceptional progress in technology. Reminded me of the following quote by Eric Schmidt

Google’s self-driving cars and robots get a lot of press, but the company’s real future is in machine learning, the technology that enables computers to get smarter and more personal. – Eric Schmidt (Google Chairman)

Grab a cup of coffee and see the future!!!


Get every new post delivered to your Inbox.

Join 1,135 other followers

%d bloggers like this: