How to make a vector with elements +1 and -1 only?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
charu shree
am 23 Jun. 2023
Beantwortet: Rik
am 23 Jun. 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 Kommentare
Akzeptierte Antwort
Parag Jhunjhunwala
am 23 Jun. 2023
Bearbeitet: Parag Jhunjhunwala
am 23 Jun. 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 Kommentare
Weitere Antworten (4)
Ronit
am 23 Jun. 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 Kommentare
Rik
am 23 Jun. 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)
fprintf('%.2f microseconds',timeit(@fun2)*1e6)
fprintf('%.2f microseconds',timeit(@fun3)*1e6)
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
Lakshay Rose
am 23 Jun. 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 Kommentare
Rahul
am 23 Jun. 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 Kommentare
Rik
am 23 Jun. 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)
Just to show this is slower than the calculated conversion (although not by too much):
timeit(@() fun_calc(sz))
timeit(@() fun_index(sz))
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 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!