How to produce 1 and -1 in MATLAB

조회 수: 1 (최근 30일)
charu shree
charu shree 2023년 7월 1일
댓글: charu shree 2023년 7월 1일
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.

답변 (1개)

Jan
Jan 2023년 7월 1일
편집: Jan 2023년 7월 1일
Exactly 50% or randomly?
x = randi([0, 1], 1, 2000) * 2 - 1;
y = [ones(1, 1000), -ones(1, 1000)];
y = y(randperm(y));
  댓글 수: 1
charu shree
charu shree 2023년 7월 1일
Thanks a lot sir for your response.... Exactly 50%

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

카테고리

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