lundi 11 septembre 2023

Recreate randperm Matlab function in Python

I have searched on stackoverflow for people facing similar issues and this topic Replicating MATLAB's `randperm` in NumPy is the most similar.

However, although it is possible to recreate the behavior of randperm function from Matlab in Python using numpy random permutation, the numbers generated are not the same, even though I choose the same seed generator for both languages. I am a bit confused since my tests were relevant for other random functions between Matlab and Python.

Here is what I have tried:

Matlab

rng(42);
randperm(15)

which returns

ans =

    11     7     6     5    15    14     1     4     9    10     3    13     8     2    12

Python

np.random.seed(42)
print(np.random.permutation(range(1,15)))

which returns

[10 12  1 13  6  9  3  2 14  5  8 11  4  7]

How can I change my Python code so it can reproduce the same order of random numbers than Matlab ?




Aucun commentaire:

Enregistrer un commentaire