[SciPy-user] Ideas about matrices

Giovanni Samaey Giovanni.Samaey at cs.kuleuven.be
Tue Nov 7 09:03:18 EST 2006


from scipy import *

size = (8,3)
A = random.random(size)
B = where(A>0.5,0.,1.)

B is your matrix

Marco wrote:
> Hi,
>
> i finally decided to keep the "import scipy" option and rewrite what needed. :)
> Just, i have a question.
>
> I am not really good at python programming (nor programming in
> general, to say), and every time i want to modify a matrix (an array)
> i have to run thru all indices.
>
> In the example, i want a matrix with random ones or zeros:
>
> import scipy
> import random
>
> jik=scipy.zeros((10,10))
> for i in range(len(jik)):
>         for k in range(len(jik)):
>                 if random.random()<0.5:
>                         jik[i,k]=1
>
> print jik
>
> Is there a more fast and synthetic way to write the same code?
> What if my matrix is not square?
>
> Sorry for my incompetence,  TIA!
>
> marco
>
> PS
> As always, RTFM much welcomed with pointers on what to read! :)
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>   



More information about the SciPy-User mailing list