How to create a vector with "n" random elements chosen from two values stated like "bit1" and "bit0"?

조회 수: 1 (최근 30일)
Hi,
Based on the code below, how do I create an vector y with "n" random elements chosen from two values bit1 and bit0? In the code below, I manually created y vector, but I want to input like n=50 and then have a "y" vector with a random sequence of bit1 and bit0 in 50 elements.
Fs=100;
Ts=1;
bit1 = ones(1,Fs);
bit0 = -ones(1,Fs);
y = [bit1 bit0 bit1 bit0 bit1];
Thank you very much,
Hugo.

답변 (1개)

per isakson
per isakson 2019년 6월 22일
Try
Fs = 100;
y = randi( [0,1], 1,Fs );
y(y==0) = -1;

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by