I wanted to modify this vector y= 3*x -1 by randomly adding or subtracting 0.5 to every element in the in it. Using rand and while loop.

 Respuesta aceptada

John D'Errico
John D'Errico el 14 de Nov. de 2017

0 votos

What vector? Is x a vector? Why do you possibly need a while loop?
y = y + round(rand(size(y))) - 0.5;
So round(rand(size(y))) is randomly 0 or 1, with 50% chance for either. Subtract 1/2, and you will effectively randomly add or subtract .5.

2 comentarios

John Doe
John Doe el 14 de Nov. de 2017
what if I want to add or substract by 0.7?
John D'Errico
John D'Errico el 15 de Nov. de 2017
y = y + round(rand(size(y)))*1.4 - 0.7;

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Nov. de 2017

Comentada:

el 15 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by