- #1
gradnu
- 21
- 0
Does anybody know how to generate a random matrix with entries 0 and 1 only(binary matrix). Numbers of 1 and 0 are fixed every time matrix is generated.
Possibly a program in 'C'.
Thanks
Possibly a program in 'C'.
Thanks
The number returned by function rand is dependent on the initial value, called a seed that remains the same for each run of a program. This means that the sequence of random numbers that is generated by the program will be exactly the same on each run of the program.
gradnu said:I want to generate random adjacency matrices where element ij is 1 if an edge connects node i to node j. Yes it is asymmetric.
To generate a binary matrix in C, you can use a nested for loop to iterate through each row and column of the matrix. Within the loop, use the rand() function to generate random numbers between 0 and 1, and then use an if statement to assign 0 or 1 to each element based on the random number generated.
Yes, you can generate a binary matrix with a specific size by using two variables to store the desired number of rows and columns. Then, use these variables in the for loop to iterate through each row and column and generate the matrix with the desired size.
To print the binary matrix in a readable format, you can use nested for loops to iterate through each row and column and use the printf() function to print each element with a space in between. You can also use formatting options, such as %d, to ensure that the matrix is printed in a clear and organized way.
Yes, you can generate a binary matrix with only 0s or 1s by using the rand() function to generate random numbers between 0 and 1, and then using an if statement to assign 0 or 1 to each element based on the random number generated. However, you can also use other methods, such as initializing the matrix with all 0s or 1s or using the bitwise operator to toggle between 0s and 1s.
A binary matrix can be useful in many applications, such as image processing, data storage, and network analysis. It can also be used as a representation of a graph or a map in various algorithms. By generating a binary matrix in C, you can efficiently store and manipulate binary data, making it a valuable tool in programming and data analysis.