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.

 채택된 답변

John D'Errico
John D'Errico 2017년 11월 14일

0 개 추천

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

John Doe
John Doe 2017년 11월 14일
what if I want to add or substract by 0.7?
John D'Errico
John D'Errico 2017년 11월 15일
y = y + round(rand(size(y)))*1.4 - 0.7;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2017년 11월 14일

댓글:

2017년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by