将随机置换代码从MATLAB移植到Python [英] Porting random permutation code from MATLAB to Python

查看:96
本文介绍了将随机置换代码从MATLAB移植到Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此MATLAB代码转换为Python?

How can this MATLAB code be translated to Python?

例如,随机文件:

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

推荐答案

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]

这篇关于将随机置换代码从MATLAB移植到Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆