Matlab Python equivalent lists

In Python, we have a convenient enumerate function:

 for i,item in enumerate(foo_list): do something 

Is there a matlab equivalent for enumerate ?

Currently, I can think of something like the following (Matlab code):

 i=1; for foo=foo_list .... i=i+1; end 
+5
source share
2 answers

As far as I know, there is no equivalent to enumeration in Matlab. The most common way to do this is:

 for i = 1:length(foo_list) item = foo_list(i); % do stuff with i, item end 
+4
source

Matlab seems to have no equivalent. However, if you have a simple 1 x X array, you can define it yourself (if you are not worried about performance):

 enumerate = @(values) [1:length(values); values] a = [6 5 4] for i=enumerate(a) do something with i end 

Of course, a clean way would be to wrap this inside a common toolkit and add the statement that a really is a 1 x X-vector.

+1
source

Source: https://habr.com/ru/post/1201009/


All Articles