Skip to content
Advertisement

Matlab to Python – Why is nested forloop running twice as often in python?

I need z to be the same index value in Python as in MATLAB, which I think means 991 in Matlab = 990 in Python.

The original MATLAB code (z = 991)

JavaScript

My Python code (z = 1980)

JavaScript

Why is my z double the amount? Where is my error? Thanks!

Advertisement

Answer

Your last line (test_timer1 += 1) needs to be indented another step. Right now it’s in your first for loop – you want it in your second.

In matlab, this doesn’t matter because you have the end statement signifying the end of your loop block. In Python, indentation is used to mark blocks.

So it should look like:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement