MATLAB Answers

using normrnd to create noise and trying to avoid loops

15 views (last 30 days)
Litalo
Litalo on 20 Jan 2021
Commented: Mathieu NOE on 20 Jan 2021
Hi ,
I have a code which contain while loop to control limits of noise i want to add, but i wonder about a way to make it faster without such loop, im sure such thing exist, so im asking here :-) !
Ok i have parameters n1 and n2 which can take values between [0 15]
and i have wgn1 and wgn2 : wgn1=normrnd(1,0), wgn2=normrnd(1,0) i want that no matter what n1 &n2 will alwyas stay in their limits
this is simple example of such code
wgn1=normrnd(0,1);
wgn2=normrnd(0,1);
% add noise
n1 = n1+sqrt(dt)*wgn1;
n2 = n2+sqrt(dt)*wgn2;
while n1>15 || n1<0
wgn1=normrnd(0,1);
n1 = n1+sqrt(dt)*wgn1;
end
while n2>15 || n2<0
wgn2=normrnd(0,1);
n2 = n2+sqrt(dt)*wgn2;
end
so i just need a better way instead of the while loops here (i dont like long simulations)
any ideas can help! :-)

  1 Comment

Mathieu NOE
Mathieu NOE on 20 Jan 2021
hello
sorry, I don't really understand the purpose of your code
rand or randn are random generator of known mean and variance value , so it should be pretty easy to multiply one or the other function by an appropriate factor to get directly what you are looking for

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by