arrays - Implementation from MATLAB to Python without iterations -


suppose have 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 want implement code in python. imagee predefined, synthetic image represented 10x10 array values 0,1,2. efficient method of accomplishing this? know iteratively walk through entire matrix , modify values go, i'm sure python has faster method that.

edit: clarify on imagee:(i have translated 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 using numpy, step in right direction. now, let's go through each statement 1 @ time , numpy equivalent of you're after. says matrix 10 x 10, , i'm going assume l = 10. here's we'll start (in ipython):

in [2]: import numpy np  in [3]: l = 10  in [4]: c= np.zeros((l,l), int)  in [5]: c[:l/2,:l/2]=1  in [6]: c[l/2:l,:l/2]=2  in [7]: c out[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 1 @ time.


[xi yi imv] = find(imagee+0.1); 

imv gives vector of values in imagee+0.1 non-zero. however, need keep in mind matlab return these values in column-major order whereas numpy same operation in row-major order. if want replicate same behaviour in python, you'll need transpose matrix first. such, i'll create new matrix transposes imagee , adds 0.1 every entry convenience. however, i'm bit confused because if imagee consists of 0,1,2, if add every value in matrix 0.1, imv return all values in imagee+0.1.... , seems pointless me. nevertheless, can use numpy.nonzero give locations of non-zero elements. once find these non-zero elements, can index transpose of c added 0.1 values want. numpy.nonzero return tuple of 2 elements first element array tells row locations of values non-zero in c+0.1 , second element array tells column locations non-zero in c+0.1:

in [9]: ct = c.t + 0.1  in [10]: ind = ct.nonzero()  in [11]: imv = ct[ind[0], ind[1]]  in [12]: imv out[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 did same operation in matlab, you'll notice imv in both python , matlab give same order of values.


imv = imv - 0.1; 

that's easy:

in [22]: imv = imv - 0.1  in [23]: imv out[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 statement (inside abs call) performing outer product of 2 vectors. in matlab, imv n x 1, , multiplying 1 x n vector of ones. can use numpy.outer outer product step. take note 1d arrays, numpy not distinguish between row vectors , column vectors , multiplying vector transpose of unfortunately won't give expect. however, if want behaviour, must explicitly define 2d matrix has singleton dimension of 1 (either rows or columns), let's put aside post.

the second part of statement performs outer product on transposed version of first part of 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]: wv out[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 code declares vector of ones convenience. after that, calculate desire.


hope helps!


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -