# How to make a vector with elements +1 and -1 only?

7 visualizzazioni (ultimi 30 giorni)
charu shree il 23 Giu 2023
Risposto: Rik il 23 Giu 2023
Hello all,
I am trying to make a vector of dimension 1X1000 with values +1 and -1 in MATLAB but not getting it correctly. Any help in this regard will be highly appreciated.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Parag Jhunjhunwala il 23 Giu 2023
Modificato: Parag Jhunjhunwala il 23 Giu 2023
The following code creates a vector of dimension 1X1000 with values -1 and +1:
vect=ones(1,1000)-2*(randi(2,1,1000)-1);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (4)

Ronit il 23 Giu 2023
Hi,
Try out the following code to create a vector of dimension 1X1000 with values +1 and -1.
for n=1:1000
A(n)= randi([-1 1]);
end
##### 2 CommentiMostra NessunoNascondi Nessuno
charu shree il 23 Giu 2023
Thank u so much Ronit sir... but cant we vectorize it...
Rik il 23 Giu 2023
As you can see, the vectorized call is about 4 times faster than the loop, but without pre-allocation it is 14 times faster.
This is of course ignoring the fact that this code produces zeros as well
fprintf('%.2f microseconds',timeit(@fun1)*1e6)
41.17 microseconds
fprintf('%.2f microseconds',timeit(@fun2)*1e6)
9.49 microseconds
fprintf('%.2f microseconds',timeit(@fun3)*1e6)
144.47 microseconds
function fun1
%proper loop
A = zeros(1,1000);
for n=1:1000
A(n)= randi([-1 1]);
end
end
function fun2
% vectorized call
A = randi([-1 1],1,1000);
end
function fun3
% No pre-allocation
for n=1:1000
A(n)= randi([-1 1]);
end
end

Accedi per commentare.

Lakshay Rose il 23 Giu 2023
Hi charu shree,
As per my understanding you are trying to make a vector of dimension [1X1000] with +1 and -1 as the only elements.
To achieve this you can use the “randi” function as shown in the below code –
vector = 2 * randi([0, 1], [1, 1000]) - 1;
You can also refer to the documentation of “randi” for further understanding –
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Rahul il 23 Giu 2023
If you are looking for creating a vector of dimension 1 x 1000 with +1 and -1 as alternate elements for the vector, then the below given code should work for you.
vector = ones(1, 1000);
vector(2:2:end) = -1;
% This vector has +1 in odd index positions and -1 in even index positions
If you want +1 to be in even index positions and -1 to be in odd index positions, then you can change the code to be
vector = ones(1, 1000);
vector(1:2:end) = -1;
If you want to make 2 separate vectors, one with +1 and other with -1, then you can try out this code
Vector_ones = ones(1, 1000);
Vector_minus_ones(1,1000)* -1;
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Rik il 23 Giu 2023
Since the other answers all take (more or less) the same approach, I wanted to add an alternative. In this specific case the overhead is not worth it, but you can use randi to select from a list of allowed values, instead of converting the list it returns to the number space you need. In this case it is easy to do the conversion, but if you have more than 2 allowed values, the answer might be tricky.
sz = [1 1000];
fun_index(sz)
ans = 1×1000
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
Just to show this is slower than the calculated conversion (although not by too much):
timeit(@() fun_calc(sz))
ans = 1.4015e-05
timeit(@() fun_index(sz))
ans = 1.7584e-05
function A=fun_calc(sz)
A = (randi([0 1],sz)-0.5)*2;
end
function A=fun_index(sz)
AllowedValues = [-1 1];
A = AllowedValues(randi(end,sz));
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by