R[1;-1] how i write the program in matlab for random selection between 1 and -1

조회 수: 2 (최근 30일)
need help

채택된 답변

Jan
Jan 2018년 1월 2일
V = 1 - 2 * randi([0,1])
or
V = 1 - 2 * (rand >= 0.5)

추가 답변 (2개)

Torsten
Torsten 2018년 1월 2일
x = rand;
if x <= 0.5
number = 1;
else
number = -1;
end
Best wishes
Torsten.

Pawel Jastrzebski
Pawel Jastrzebski 2018년 1월 2일
clear all;
clc;
% use 'random function' of your choice
% - rand()
% - randi()
% - randn()
% GENERATES A RANDOM NUMBER IN A RANGE OF -1 TO 1:
r1 = -1+ 2*rand(1);
% GETS THE SIGN OF THE 'r1'
sVal = sign(r1);
% ALL ABOVE CAN BE DONE IN ONE LINE:
sVal1 = sign(-1+ 2*rand(1));

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by