I believe that there is no way to do this. The trick I used in the past is to exploit the fact that Matlab iterates over the columns of a matrix, so you can define an enumerate function that adds an index row to the beginning of the matrix:
function output = enumerate(x) output = [1:size(x,2); x]; end
and then use it like this:
for tmp = enumerate(items) index = tmp(1); item = tmp(2:end); end
but this is actually no better than what you originally did. It would be nice if in Python you could something like
for [index,item] = enumerate(items) # loop body end
where enumerate is a function that returns two matrices of the same length, but ... you cannot.
Chris taylor
source share