MATLAB Answers

How to assign two values randomly among 2 variables?

7 ビュー (過去 30 日間)
Bhavya Gandham
Bhavya Gandham 2016 年 10 月 6 日
コメント済み: Bhavya Gandham 2016 年 10 月 7 日
c1 = (b1-a1).*rand(100,1) + a1; c2 = (b2-a2).*rand(100,1) + a2; I have to assign the c1 and c2 values randomly among two variables sensor1 and sensor2. How can I do this? Thanks in advance...

  3 件のコメント

KSSV
KSSV 2016 年 10 月 6 日
sensor1 = (b1-a1).*rand(100,1) + a1;
sensor2 = (b2-a2).*rand(100,1) + a2;
wont it work? As the values are random, define sensor values itself random.
Bhavya Gandham
Bhavya Gandham 2016 年 10 月 6 日
the ranges of c1 and c2 are different. c1 range is (0,0.7) and c2 range is 90.7,1.2) I have to assign these random values to sensor1 and sensor2 randomly. thanks in advance...
Bhavya Gandham
Bhavya Gandham 2016 年 10 月 7 日
thank you!

Sign in to comment.

回答 (2 件)

elias GR
elias GR 2016 年 10 月 6 日
編集済み: elias GR 2016 年 10 月 6 日
r=rand; %random number between 0 and 1
if r<0.5
sensor1 = c1;
sensor2 = c2;
else
sensor1 = c2;
sensor2 = c1;
end

  0 件のコメント

Sign in to comment.


Thorsten
Thorsten 2016 年 10 月 6 日
idx = rand(100,1);
sensor1 = c1;
sensor1(idx>0.5) = c2;
sensor2 = c1;
sensor2(idx<=0.5) = c2;

  0 件のコメント

Sign in to comment.


Translated by