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
#2
from itertools import permutations
import random

rows = list(permutations([-1, 0, 1], 3))
matrix = np.array([random.choice(rows) for _ in range(1000)])
print(matrix)

if you wish you can pass matrix as argument to np.array()
Sorry, i misunderstood a bit

import random

rows = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 0, -1], [0, -1, 0], [-1, 0, 0]]
matrix = np.array([random.choice(rows) for _ in range(1000)])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you for the answer.

The matrix is not exactly what I want (only one non-zero element is wanted in each row), but I was able to modify it. Instead of using the permutations() function I typed this: rows=list([-1, 0, 0], [0,-1,0], [0,0,-1], [1,0,0], [0,1,0], [0,0,1])

It is working well
Reply
#4
(Aug-28-2018, 05:06 PM)PhysChem Wrote: The matrix is not exactly what I want (only one non-zero element is wanted in each row)
yaeh, I realised that and provided additional code :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
No problem, once you told me the random.choice() function, I got what I needed.

Is there any difference between list([blabla], [bla]) and [[blabla], [bla]] ?

Or the interpreter handle them as synonyms, and the processor will do the same things (and it takes the same time)? I have to generate this matrix many times, so speed is important.
Reply
#6
>>> list(['blabla'], ['bla'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (2 given)
what you suggest is not valid code as you can see. list() takes one iterable as argument.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
You are right!

I wrote the 3th post quite after I quitted from the python3, and I didn't used the copy-past. Now I reviewed, what was typed in the terminal. I have used the rows=[[-1, 0, 0], [0,-1,0], [0,0,-1], [1,0,0], [0,1,0], [0,0,1]] command, just like you. And that worked, while the list() didn't
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  define a diagonal matrix from a matrix amalalaoui 1 2,328 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