Generating NxN Matrix with Random 0s and 1s - MATLAB M File Help

In summary, the conversation is about generating a NxN matrix that is 25% filled with 0s and 1s in a random pattern. The person is also asking for help in selecting a 3x3 matrix centered around a specific point in the matrix using indexing. They are also trying to figure out how to generate the random matrix as described.
  • #1
3
0
i am writing a MATLAB m file.

i need to generate a NxN matrix 25% filled with 0s and 1s in a random pattern...

is this at all possible?

also, in an NxN matrix, i need to select a 3x3 matrix centered around a certain point, can you help me out with this?

many thanks for your help
 
Physics news on Phys.org
  • #2
shawaj said:
also, in an NxN matrix, i need to select a 3x3 matrix centered around a certain point, can you help me out with this?

many thanks for your help

Isn't this just a question of indexing the array appropriately? I don't remember MATLAB syntax exactly, but often in these computational languages, indexing of an array is achieved by means of square brackets [] or parentheses (). So if your array is called 'arr' and the point of interest is at column i, row j (or vice versa, depending on the convention), you would index it as arr[i,j]. Now you just need to range from i-1 to i+1 and from j-1 to j+1. I don't remember what MATLAB uses to mean "range from", but it might be something like a:b to mean "range from a to b" with the colon replaced by whatever symbol MATLAB uses for this purpose. Then your 3x3 sub-array would be given by arr[i-1:i+1, j-1:j+1].
 
  • #3
I will try that out!

Any ideas on the other one? Generating a random matrix of 0s and 1s?
 
  • #4
many thanks for your help...cepheid

wasn't exactly what i was looking for but it helped me to find what i was looking for and have sorted that part of it now.

still trying to work out how to generate the random matrix as detailed above
 
  • #5


I can confirm that it is possible to generate a NxN matrix with a random pattern of 0s and 1s using MATLAB. You can use the function "randi" to generate a random matrix with values between 0 and 1, and then use the "floor" function to round the values to either 0 or 1. For example, the code "A = floor(randi([0,1],N,N))" will generate a NxN matrix filled with random 0s and 1s.

To generate a matrix with a specific percentage of 0s and 1s, you can use the "rand" function to generate a matrix with values between 0 and 1, and then use the "round" function to round the values to either 0 or 1. For example, the code "A = round(rand(N,N)*0.25)" will generate a NxN matrix with 25% 1s and 75% 0s.

To select a 3x3 matrix centered around a certain point in a NxN matrix, you can use the "ceil" and "floor" functions to define the range of rows and columns, and then use the ":" operator to select the desired submatrix. For example, to select the 3x3 matrix centered around the point (i,j), the code would be "B = A(ceil(i-1.5):floor(i+1.5),ceil(j-1.5):floor(j+1.5))".

I hope this helps. Best of luck with your MATLAB m file!
 

1. How do I generate an NxN matrix with random 0s and 1s using MATLAB?

To generate an NxN matrix with random 0s and 1s using MATLAB, you can use the "randi" function. This function generates a matrix of random integers between specified values. For example, you can use "randi([0,1],N)" to generate an NxN matrix with random 0s and 1s.

2. Can I specify the proportion of 0s and 1s in the generated matrix?

Yes, you can specify the proportion of 0s and 1s in the generated matrix by using the "probabilities" parameter in the "randi" function. For example, you can use "randi([0,1],N,'probabilities',[0.3,0.7])" to generate an NxN matrix with 30% 0s and 70% 1s.

3. How can I save the generated matrix as a variable in MATLAB?

To save the generated matrix as a variable in MATLAB, you can use the "assignin" function. This function allows you to assign a value to a variable in the MATLAB workspace. For example, you can use "assignin('base','my_matrix',randi([0,1],N))" to save the generated matrix as a variable named "my_matrix" in the base workspace.

4. Is it possible to generate a matrix with random numbers other than 0s and 1s?

Yes, you can generate a matrix with random numbers other than 0s and 1s by using the "rand" function. This function generates a matrix of random numbers between 0 and 1. For example, you can use "rand(N)" to generate an NxN matrix with random decimal numbers.

5. How can I control the randomness of the generated matrix?

You can control the randomness of the generated matrix by using the "rng" function. This function allows you to set the seed for the random number generator in MATLAB. By setting a specific seed, you can ensure that the same random numbers are generated each time. This can be useful for replicating results or for testing purposes.

Suggested for: Generating NxN Matrix with Random 0s and 1s - MATLAB M File Help

Replies
1
Views
915
Replies
1
Views
887
Replies
5
Views
979
Replies
3
Views
711
Replies
8
Views
1K
Replies
2
Views
1K
Replies
1
Views
761
Back
Top