Randomly Assigning 0 or 1 to a Vector in MATLAB

  • Thread starter Thread starter p-williams65
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary
SUMMARY

The discussion focuses on a MATLAB task involving the random assignment of 0 or 1 to a column vector of 100 entries, repeated for a total of 1 million iterations. The user initially attempted to use the command h(round(rand(1)*100)) = randint(1) + h(round(rand(1)*100)) but struggled with implementing a for loop for automation. Ultimately, the user resolved the issue, confirming that the problem was not with the for loop itself.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with random number generation in MATLAB
  • Knowledge of vector manipulation in MATLAB
  • Experience with control structures, specifically for loops in MATLAB
NEXT STEPS
  • Explore MATLAB's rand and randi functions for random number generation
  • Learn about vector indexing and manipulation in MATLAB
  • Study control structures in MATLAB, focusing on for loops
  • Investigate performance optimization techniques for large iterations in MATLAB
USEFUL FOR

Students and educators in programming, particularly those learning MATLAB for data manipulation and randomization tasks.

p-williams65
Messages
7
Reaction score
0

Homework Statement



Hi. The problem assigned to me is to take a column vector of 100 entries and then to randomly assign add a 0 or 1 to a random entry, and then to repeat this "n" # of times, where the upper "n" is 1 million.

I have this so far

h = zeros(100,1)
h(round(rand(1)*100)) = randint(1) + h(round(rand(1)*100))

If I manually copy and paste the bottom command in MATLAB, it seems to work fine, but obviously I can't copy and paste this a million times. I don't know how to implement a "for" loop exactly, for this cause. Any help would be greatly appreciated, thanks.
 
Physics news on Phys.org
Look at the other thread you posted. In that thread you were given an example of how to implement a for loop in matlab.
 
Figured it out; the problem wasn't the for loop. Thx.
 
Last edited:

Similar threads

Replies
2
Views
2K
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
15
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K