Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab Is there something like list comprehension as it is in python?

Tags:

arrays

matlab

I am looking for something like list comprehensions in matlab however I couldnt find anything like this in the documentary.

In python it would be something like

A=[i/50 for i in range(50)]
like image 856
bios Avatar asked Nov 30 '11 13:11

bios


2 Answers

You can do:

(1:50)/50

Or for something more general, you can do:

f=@(x) (x/50);
arrayfun(f,1:50)
like image 51
Oli Avatar answered Nov 03 '22 13:11

Oli


Matlab is very fond of 'vectorizing'. You would write your example as:

A = (0:49) ./ 50

Matlab hates loops and therefore list comprehension. That said, take a look at the arrayfun function.

like image 13
Marijn van Vliet Avatar answered Nov 03 '22 12:11

Marijn van Vliet