Matlab Is there something like list comprehension like in python?

I was looking for something like understanding lists in Matlab, but I could not find anything like this in a documentary.

In python, it will be something like

A=[i/50 for i in range(50)] 
+6
source share
6 answers

Matlab is very fond of "vectorization." You should write your example:

 A = (0:49) ./ 50 

Matlab hates loops and therefore understands the list. However, look at the arrayfun function.

+10
source

You can do:

 (1:50)/50 

Or for something more general, you can do:

 f=@ (x) (x/50); arrayfun(f,1:50) 
+5
source

No, Matlab has no listings. You really don't need this, as the focus should be on array-level computing:

 A = (1:50) / 50 
+2
source

If what you are trying to do is as trivial as the sample, you can just do scalar separation:

 A = (0:50) ./ 50 
0
source

There are several ways to create a list in Matlab that goes from 0 to 49/50 in increments of 1/50

 A = (0:49)/50 B = 0:1/50:49/50 C = linspace(0,49/50,50) 

EDIT As Sam Roberts pointed out in the comments, although all of these lists should be equivalent, the numerical results are different from floating point errors. For instance:

 max(abs(AB)) ans = 1.1102e-16 
0
source

Matlab can work with arrays directly, making list comprehension less useful.

-1
source

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


All Articles