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

1 回表示 (過去 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));
  2 件のコメント
Jan
Jan 2018 年 1 月 2 日
The sign() function can reply 0 also, if rand replies 0.5.
Pawel Jastrzebski
Pawel Jastrzebski 2018 年 1 月 2 日
Good point - thanks for correcting.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by