本文介绍了从MATLAB实现到Python没有迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设你有MATLAB以下code: [希夷IMV] =找到(imagee + 0.1);    IMV IMV = - 0.1;    WV = ABS(IMV *一(1,长度(IMV)) - 酮(长度(IMV),1)* IMV'); 和要实现的Python这code。 Imagee是pdefined一个$ P $,再由10×10阵列值0,1,2 psented $ P $合成图像。什么是解决这个问题的最有效的方法?我知道你可以通过整个矩阵反复行走,当您去修改值,但我敢肯定,Python有比这更快的方法。 编辑:澄清imagee:(我有翻译这蟒蛇已经) C = np.zeros((L,L),INT)    C [L / 2,L / 2] = 1    C [L / 2:L,L / 2] = 2 解决方案 我看到你已经在使用 numpy的,这是朝着正确方向迈出的一步。现在,让我们通过每个语句一次并得到你以后的 numpy的等价的。它说,你的矩阵是 10×10 ,所以我会假设 L = 10 。下面就是我们将与开始(在IPython中): [2]:进口numpy的为NP在[3]:L = 10在[4]:C = np.zeros((L,L),INT)在[5]:C [L / 2,L / 2] = 1在[6]:C [L / 2:L,L / 2] = 2[7]:C出[7]:阵列([[1,1,1,1,1,0,0,0,0,0],       [1,1,1,1,1,0,0,0,0,0],       [1,1,1,1,1,0,0,0,0,0],       [1,1,1,1,1,0,0,0,0,0],       [1,1,1,1,1,0,0,0,0,0],       [2,2 2,2 2,0,0,0,0,0],       [2,2 2,2 2,0,0,0,0,0],       [2,2 2,2 2,0,0,0,0,0],       [2,2 2,2 2,0,0,0,0,0],       [2,2 2,2 2,0,0,0,0,0]]) 现在,让我们通过每行一次。 [希夷IMV] =找到(imagee + 0.1); IMV 基本上给你所有值的矢量imagee + 0.1 是非零。不过,你需要牢记什么是MATLAB将在列主为了将这些值,而 numpy的会做同样的操作在行主订单。如果要复制在Python相同的行为,你需要为置矩阵第一。因此,我将创建一个调换 imagee ,并增加了0.1,为方便每个条目一个新的矩阵。不过,我有点糊涂了,因为如果 imagee 已经由 0,1,2 ,如果添加的每在这个矩阵值0.1, IMV 将返回全部在值imagee + 0.1 。 ......这似乎毫无意义了我。不过,你可以使用 numpy.nonzero 给你非零元素的位置。一旦你找到这些非零元素,你可以简单地索引到 C 的转加入 0.1 来获得重视你想要的。 numpy.nonzero 将返回两个元素,其中第一个元素是,告诉你,在 C + 0.1 ,第二个元素是一个数组,它告诉你,在 C + 0.1 : 在[9]:CT = C.T + 0.1在[10]:的ind = CT.nonzero()在[11]:IMV = CT [的ind [0],的ind [1]][12]:IMV出[12]:阵列([1.1,1.1,1.1,1.1,1.1,2.1,2.1,2.1,2.1,2.1,1.1,    1.1,1.1,1.1,1.1,2.1,2.1,2.1,2.1,2.1,1.1,1.1,    1.1,1.1,1.1,2.1,2.1,2.1,2.1,2.1,1.1,1.1,1.1,    1.1,1.1,2.1,2.1,2.1,2.1,2.1,1.1,1.1,1.1,1.1,    1.1,2.1,2.1,2.1,2.1,2.1,0.1,0.1,0.1,0.1,0.1,    0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,    0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,    0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,    0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,    0.1]) 如果你没有在MATLAB中相同的操作,你会发现, IMV 在Python和MATLAB给出值的顺序相同。 IMV IMV = - 0.1; 这很简单: 在[22]:IMV = IMV - 0.1在[23]:IMV出[23]:阵列([1,1,1,1,1,2,2,2,2,2,1。,1.,1.,        1.,1.,2,2,2,2,2,1。,1.,1.,1。,1.,2,        2.,2.,2,2,1。,1.,1.,1。,1.,2,2,2,2,        2. 1.,1.,1。,1.,1.,2,2,2,2,2,0,0,        0,0,0,0,0,0,0,0,0,0,0,0,0,        0,0,0,0,0,0,0,0,0,0,0,0,0,        0,0,0,0,0,0,0,0,0,0,0,0,0,        0,0,0,0,0,0,0,0,0]) WV = ABS(IMV *一(1,长度(IMV)) - 那些(长(IMV),1)* IMV'); 这句话的第一部分正在执行的 ABS 里面调用)=htt​​p://en.wikipedia.org/wiki/Outer_product相对=nofollow>两个向量的外积。在MATLAB中, IMV 是的N×1 ,以及你是一个乘以这个1那些的×N的载体。您可以使用 numpy.outer 来帮助你做到这一点外积的一步。需注意,对于一维数组, numpy的行向量和列向量等乘以另一个的转矢量遗憾的是不会给你你所期望的不区分。但是,如果你想要这种行为,你必须明确地定义为1(或行或列)一个单维度2D矩阵,但让我们把这些抛开这个职位。本声明的第二部分也执行外的产品,但在声明中的第一部分的转版本。因此​​: 在[24]:ones_vector = np.ones(LEN(IMV))在[25]:WV = np.abs(np.outer(IMV,ones_vector) - np.outer(ones_vector,IMV))在[26]:WV出[26]:阵列([0,0,0,...,1,1,1],   [0,0,0,...,1,1,1],   [0,0,0,...,1,1,1],   ...   [1.,1.,1。,...,0,0,0],   [1.,1.,1。,...,0,0,0],   [1.,1.,1。,...,0,0,0。]]) 的 code的第一部分声明那些为了方便的载体。在那之后,我们计算你的愿望。希望这有助于!Suppose you have the following code in MATLAB: [xi yi imv] = find(imagee+0.1); imv = imv - 0.1; wv = abs(imv*ones(1,length(imv)) - ones(length(imv),1)*imv');And you want to implement this code in Python. Imagee is a predefined, synthetic image represented by a 10x10 array with values 0,1,2. What would be the most efficient method of accomplishing this? I know you could iteratively walk through the entire matrix and modify values as you go, but I'm sure python has a faster method than that. EDIT: to clarify on imagee:(I have translated this to python already) C= np.zeros((L,L), int) C[:L/2,:L/2]=1 C[L/2:L,:L/2]=2 解决方案 I see you're already using numpy, which is a step in the right direction. Now, let's go through each statement one at a time and get the numpy equivalent of what you're after. It says that your matrix is 10 x 10, and so I'm going to assume that L = 10. Here's what we'll start with (in IPython):In [2]: import numpy as npIn [3]: L = 10In [4]: C= np.zeros((L,L), int)In [5]: C[:L/2,:L/2]=1In [6]: C[L/2:L,:L/2]=2In [7]: COut[7]:array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], [2, 2, 2, 2, 2, 0, 0, 0, 0, 0], [2, 2, 2, 2, 2, 0, 0, 0, 0, 0], [2, 2, 2, 2, 2, 0, 0, 0, 0, 0], [2, 2, 2, 2, 2, 0, 0, 0, 0, 0], [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]])Now, let's go through each line one at a time.[xi yi imv] = find(imagee+0.1);imv basically gives you a vector of all values in imagee+0.1 that are non-zero. However, what you need to keep in mind is that MATLAB will return these values in column-major order whereas numpy will do the same operation in row-major order. If you want to replicate the same behaviour in Python, you'll need to transpose the matrix first. As such, I'll create a new matrix which transposes imagee and adds 0.1 to every entry for convenience. However, I'm a bit confused because if imagee already consists of 0,1,2, if you add every value in this matrix by 0.1, imv will return all values in imagee+0.1.... and that seems pointless to me. Nevertheless, you can use numpy.nonzero to give you the locations of the non-zero elements. Once you find these non-zero elements, you can simply index into the transpose of C added with 0.1 to get the values you want. numpy.nonzero will return a tuple of two elements where the first element is an array that tells you the row locations of those values that were non-zero in C+0.1 and the second element is an array that tells you the column locations that were non-zero in C+0.1:In [9]: CT = C.T + 0.1In [10]: ind = CT.nonzero()In [11]: imv = CT[ind[0], ind[1]]In [12]: imvOut[12]:array([ 1.1, 1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 2.1, 1.1, 1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 2.1, 1.1, 1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 2.1, 1.1, 1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 2.1, 1.1, 1.1, 1.1, 1.1, 1.1, 2.1, 2.1, 2.1, 2.1, 2.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])If you did the same operation in MATLAB, you'll notice that imv in both Python and MATLAB give the same order of values.imv = imv - 0.1;That's easy:In [22]: imv = imv - 0.1In [23]: imvOut[23]:array([ 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])wv = abs(imv*ones(1,length(imv)) - ones(length(imv),1)*imv');The first part of this statement (inside the abs call) is performing the outer product of two vectors. In MATLAB, imv would be N x 1, and you are multiplying this with a 1 x N vector of ones. You can use numpy.outer to help you do this outer product step. Take note that for 1D arrays, numpy does not distinguish between row vectors and column vectors and so multiplying a vector with the transpose of another unfortunately won't give you what you expect. However, if you want this behaviour, you must explicitly define a 2D matrix that has a singleton dimension of 1 (either the rows or the columns), but let's put that aside for this post.The second part of this statement also performs an outer product but on the transposed version of the first part of the statement.Therefore:In [24]: ones_vector = np.ones(len(imv))In [25]: wv = np.abs(np.outer(imv, ones_vector) - np.outer(ones_vector, imv))In [26]: wvOut[26]:array([[ 0., 0., 0., ..., 1., 1., 1.], [ 0., 0., 0., ..., 1., 1., 1.], [ 0., 0., 0., ..., 1., 1., 1.], ..., [ 1., 1., 1., ..., 0., 0., 0.], [ 1., 1., 1., ..., 0., 0., 0.], [ 1., 1., 1., ..., 0., 0., 0.]])The first part of the code declares a vector of ones for convenience. After that, we calculate what you desire.Hope this helps! 这篇关于从MATLAB实现到Python没有迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 20:20