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

조회 수: 11 (최근 30일)
charu shree
charu shree 2023년 6월 23일
답변: Rik 2023년 6월 23일
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.

채택된 답변

Parag Jhunjhunwala
Parag Jhunjhunwala 2023년 6월 23일
편집: Parag Jhunjhunwala 2023년 6월 23일
The following code creates a vector of dimension 1X1000 with values -1 and +1:
vect=ones(1,1000)-2*(randi(2,1,1000)-1);

추가 답변 (4개)

Ronit
Ronit 2023년 6월 23일
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
charu shree
charu shree 2023년 6월 23일
Thank u so much Ronit sir... but cant we vectorize it...
Rik
Rik 2023년 6월 23일
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

댓글을 달려면 로그인하십시오.


Lakshay Rose
Lakshay Rose 2023년 6월 23일
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 –

Rahul
Rahul 2023년 6월 23일
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;

Rik
Rik 2023년 6월 23일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by