MATLAB Answers

using normrnd to create noise and trying to avoid loops

15 views (last 30 days)
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
% add noise
n1 = n1+sqrt(dt)*wgn1;
n2 = n2+sqrt(dt)*wgn2;
while n1>15 || n1<0
n1 = n1+sqrt(dt)*wgn1;
while n2>15 || n2<0
n2 = n2+sqrt(dt)*wgn2;
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
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