Python Forum
random matrix, with some special feature
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random matrix, with some special feature
#1
Hi!

I wish to create a random 1000*3 sized (1000 row & 3 column) matrix with the following conditions:

1) the matrix must be filled only with the following elements: -1; 0; 1
2) in every row-vector, the 0-number must be there exactly two times

---I don't speak english good, so maybe the 2 sentence above makes no sense. What i want: the length (I mean the Eukleidesian norm) of these row-vectors must be exactly 1 ---

3) within each row-vector the position of the non-zero element must be random; and the probability that the non-zero element is in the first place == probability that it is in the second place == probability that it is in the third place (and the probability that the non-zero element is -1 == the probability that it is +1)


I don't have too much experience in Python (nor generally in programming...)

The best (but still bad) idea I have:


Step_1 Define the 6 different row vector: (a=np.matrix([1,0,0]) ; b=np.matrix([0,1,0]) ; and so on ; f=np.matrix([0,0,-1])


Step_2 (Creating the first row): With random.randint(1, 6) I will get a random number. For example if it is 1, then the first row-vector of my big matrix will be the [1,0,0]. If it is 6, then the first row-vector will be the f-vector [0,0,-1]
I think it can be done in way:

import random as random
v=random.randint(1, 6)
if v==1:
first_row_vector=a
elif v=2:
first_row_vector=b
### and so on, I am lazy to type...
elif v=6:
first_row_vector=f


Step_3 "glue together" the first_row_vector and the next (random) row-vector (called sorvektor) into a matrix, using the np.concatenate() command. Once it is done, then attach the next row-vector to the 2*3 matrix. And so on, until i have a 1000*3 matrix.
>>> for n in range(0,998):
... v=random.randint(1,6)
... if v==1:
... sorvektor=a
... elif v==2:
... sorvektor=b
... elif v==3:
... sorvektor=c
... elif v==4:
... sorvektor=d
... elif v==5:
... sorvektor=e
... elif v==6:
... sorvektor=f
... matrix=np.concatenate((first_row_vector, sorvektor), axis=0)
... print(matrix)

Well... it works, but not as I want...
Reply


Messages In This Thread
random matrix, with some special feature - by PhysChem - Aug-28-2018, 03:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  define a diagonal matrix from a matrix amalalaoui 1 2,432 May-15-2019, 01:12 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020