Special Random Numbers Matlab

Hi Guys,

I am new to this group (and Matlab in general), but I have been working on a project recently and I need help on it. I am trying to write a ML subroutine/function to generate a "random" array with equal probability of +1 and -1. What I really want is an array with specified dimensions (inout by user), which has equal numbers of 1's and -1's in it. but the 1's and -1's must be randomly distributed.

Of course a simple way of doing this would be to run through the entire array, and write an 'if' statement for each element of the array, as follows:
a=rand (4)
for x=1:16 (%Total length of the array)
if a(x)>0.5, then print 1, if not, print -1.
But the problem is: In the rand function, more than half of the numbers are less (or greater) than 0.5. So the subroutine I just wrote above fails spectacularly. Can anyone help me? Here's the problem again: I need to generate an array (via a function/subroutine) that has half it's elements as 1, and the other half as -1. What's the most random way of doing this (without the for loop I suggested above). Any ideas, anyone?
Depserately need help.

thanks.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories