Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Porting random permutation code from MATLAB to Python

Tags:

python

matlab

How can this MATLAB code be translated to Python?

For example with random files:

FileA = rand([10,2]);
FileB = randperm(10);

for i=1:10
fileC(FileB(i),1)=FileA(i,1); %for the x
fileC(FileB(i),2)=FileA(i,2); %for the y
end
like image 357
Boris Avatar asked Feb 22 '23 09:02

Boris


1 Answers

import numpy as np
array_a = np.random.rand(10,2)
array_b = np.random.permutation(range(10))

array_c = np.empty(array_a.shape, array_a.dtype)
for i in range(10):
    array_c[array_b[i], 0] = array_a[i, 0]
    array_c[array_b[i], 1] = array_a[i, 1]
like image 167
wim Avatar answered Mar 06 '23 09:03

wim