Nani
Nani

Reputation: 1

Analogue to matlab function "randsrc"

I am looking for a function in Python that is similar to randsrc in MATLAB.

I would like to rewrite:

data_points=32;
M=4;
data_source = randsrc(1, data_points, mslice[0:M - 1]);

What is the analogue to randscrc in Python?

Upvotes: 0

Views: 797

Answers (1)

vurmux
vurmux

Reputation: 10020

Python has random module you can use. If you want to construct a list of random elements (as in your example), you can use random.choice and list generator:

[random.choice([1,2,'A',4]) for _ in range(15)]

which will return you:

['A', 1, 'A', 1, 1, 4, 1, 2, 1, 4, 2, 4, 4, 2, 1]

If you want a column of random elements, add brackets in generator:

[[random.choice([111,2,333,4])] for _ in range(15)]

[[2],
 [333],
 [4],
 [2],
 [4],
 [2],
 [111],
 [333],
 [333],
 [333],
 [111],
 [333],
 [2],
 [111],
 [2]]

If you want a random matrix, expand the generator:

[[random.choice([1,2,3,4]) for _ in range(5)] for _ in range(5)]

[[1, 2, 4, 3, 4],
 [4, 2, 1, 1, 1],
 [3, 1, 2, 1, 3],
 [1, 2, 3, 3, 3],
 [2, 1, 3, 1, 2]]

You can store any elemets in the choices "alphabet".

Upvotes: 1

Related Questions