Randomly Assigning 0 or 1 to a Vector in MATLAB

  • Thread starter p-williams65
  • Start date
  • Tags
    Matlab
In summary, The problem is to randomly assign a 0 or 1 to a random entry in a column vector of 100 entries, and repeat this process 1 million times. The given code works for a single iteration, but a for loop is needed to automate the process. The solution was provided in a previous thread.
  • #1
p-williams65
7
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
  • #2
Look at the other thread you posted. In that thread you were given an example of how to implement a for loop in matlab.
 
  • #3
Figured it out; the problem wasn't the for loop. Thx.
 
Last edited:

1. How can I repeat a command multiple times in MATLAB?

To repeat a command multiple times in MATLAB, you can use a for loop or a while loop. Both of these loops allow you to specify the number of times you want the command to be repeated.

2. Is there a way to repeat a command until a certain condition is met in MATLAB?

Yes, you can use a while loop to repeat a command until a certain condition is met. The loop will continue to run until the condition evaluates to false.

3. Can I use a specific number of iterations when repeating a command in MATLAB?

Yes, you can specify the number of iterations in a for loop to repeat a command a specific number of times. This is useful when you know exactly how many times you want the command to be repeated.

4. How do I stop a command from repeating in MATLAB?

To stop a command from repeating in MATLAB, you can use the break keyword inside a loop. This will immediately terminate the loop and move on to the next line of code.

5. Can I repeat a command with different inputs in MATLAB?

Yes, you can use a for loop to repeat a command with different inputs. You can store the inputs in an array and use the loop index to access each input during each iteration of the loop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Engineering and Comp Sci Homework Help
Replies
1
Views
960
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
575
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
Back
Top