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

Click For Summary

Discussion Overview

The discussion revolves around generating an NxN matrix filled with random 0s and 1s in MATLAB, specifically targeting a 25% fill rate. Additionally, participants are exploring how to select a 3x3 sub-matrix centered around a specified point within the larger matrix.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant inquires about the feasibility of generating a random NxN matrix that is 25% filled with 0s and 1s.
  • Another participant suggests that selecting a 3x3 matrix centered around a specific point can be achieved through appropriate indexing, mentioning the use of square brackets or parentheses for array indexing in MATLAB.
  • A participant expresses intent to try the indexing approach for selecting the 3x3 matrix and seeks further assistance on generating the random matrix.
  • One participant acknowledges the previous help but indicates that it did not fully address their needs regarding the random matrix generation.

Areas of Agreement / Disagreement

Participants generally agree on the indexing method for selecting a sub-matrix, but the method for generating the random matrix remains unresolved, with no consensus on a specific approach presented.

Contextual Notes

There are limitations regarding the specific MATLAB syntax for generating the random matrix and the exact method to achieve a 25% fill rate, which have not been fully explored or resolved in the discussion.

shawaj
Messages
3
Reaction score
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
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].
 
I will try that out!

Any ideas on the other one? Generating a random matrix of 0s and 1s?
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
35
Views
8K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
19K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K