MATLAB: I want to generate ones in consecutive random positions in a vector of zeros for example i want to generate 5 ones in consecutive positions in a specific range lets say from position 5 to position 15 assuming that the vector has 48 positions

generate random number of ones in a consecutive positions of vector within specified rangeMATLAB

i want to generate ones in consecutive random positions in a vector of zeros for example i want to generate 5 ones in consecutive positions in a specific range lets say from position 5 to position 15 assuming that the vector has 48 positions

Best Answer

Start with a vector of zeros.
Now, if you want sequence of 5 ones, starting from some random point, then just pick the start point at random. Use randi to do that. Then overwrite the indicated 5 consecutive zeros with ones.
Related Question