How to produce 1 and -1 in MATLAB

Hello all, I want to generate 2000 numbers out of which 50% times it should be +1 and for remaining 1000 it should be -1.
Any help in this regard will be highly appreciated.

Answers (1)

Jan
Jan on 1 Jul 2023
Edited: Jan on 1 Jul 2023
Exactly 50% or randomly?
x = randi([0, 1], 1, 2000) * 2 - 1;
y = [ones(1, 1000), -ones(1, 1000)];
y = y(randperm(y));

1 Comment

Thanks a lot sir for your response.... Exactly 50%

Sign in to comment.

Categories

Find more on Random Number Generation in Help Center and File Exchange

Asked:

on 1 Jul 2023

Commented:

on 1 Jul 2023

Community Treasure Hunt

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

Start Hunting!