MATLAB: Generating vectors of length N with only -1s and 1s.

randomvectors

I want to generate some M number of vectors of length N with the entries restricted to -1s and 1s and the vectors should not be repetitive. Is it possible to do so? If yes, how?

Best Answer

  • >> M = 5;
    >> N = 19;
    >> randi(0:1,M,N)*2-1
    ans =
    -1 1 -1 1 -1 -1 1 -1 1 -1 -1 1 -1 1 1 -1 -1 1 1
    1 -1 -1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1 1 1 -1 1
    -1 1 -1 1 1 1 1 -1 1 -1 -1 -1 1 -1 1 1 1 1 -1
    1 -1 -1 1 -1 1 -1 -1 -1 -1 -1 1 -1 1 -1 1 1 1 -1
    -1 -1 1 -1 -1 -1 1 1 1 1 1 -1 -1 1 1 1 1 1 1