Is there a matlab loop equivalent to a for in loop in python?

Is there a matlab equivalent of a for for loop in python?

For example, in python, I can iterate over list items using the following code:

for c_value in C_VALUES: 
+4
source share
2 answers

Matlab for iterates over values โ€‹โ€‹in a row vector. Just like your example if C_VALUES were a string.

 for val = row_vec #% stuff in the loop end 

- matlab syntax. val will take the row_vec values โ€‹โ€‹as it iterates. The syntax you'll often see (but not necessarily) is

 for ii = 1:length(values) val = values(ii); #% stuff in the loop using val end 

Here 1:length(values) creates the string vector [1 2 3 ...] , and ii can be used for indexing in values .

(Note: i is another common choice, but as soon as you use i in this type of context where it is assigned a value, you can no longer use it in an imaginary number).

+10
source

Please try the following code.

  vs = [1 12 123 1234]; for v = vs disp(v) end 
0
source

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


All Articles