jeudi 7 juillet 2016

Python if function across a vector

I'm new to Python and trying to work out how to do the following.

I have a data array, X, which looks something like:

X = [ 1 2 3

      4 5 6 

      7 8 9 

      10 11 12];

Such that the first column X[0] is,

X[0] =[1
       4
       7
       10];

the second column X[1] is,

X[1] =[2
       5
       8
       11];

and third column X[2] is,

X[2] =[3
       6
       9
       12];

What I want to do is generate a single random number, k, and if any of the values in the first column X[0] are greater than k I want to multiply the third column by a function (2*X[0]), and if any of the values are less than k I want to multiply by a different function (2*k).

So if k=2, the third column becomes: and third column X[2] is,

X[2] =[3*2*k
       6*2*X[0,1]
       9*2*X[0,2]
       12*2*X[0,3] ];

Is there a succinct way to do this?

Aucun commentaire:

Enregistrer un commentaire