MATLAB: Creating [-1 1] matrix given length n

mathematicsMATLABmatrix

Hi,
I want to create N length long [-1 1] matrix,
For example: Length=2
Output:
[ 1 1
1 -1
-1 1
-1 -1]
Length = 3
Output:
[ 1 1 1
1 1 -1
1 -1 1
-1 1 1
......
-1 -1 -1]
And so on, How can do that?

Best Answer

Hi Alice,
You can try the following:
len = 2;
N = 2^len;
bi = de2bi(N-1:-1:0); % Convert decimal numbers in to binary
bi(bi==0) = -1; % Replace the values of 0 with -1
Hope this helps.
Regards,
Sriram