random add or subtract a number to an array

조회 수: 3 (최근 30일)
Manikatam Gaddam
Manikatam Gaddam 2019년 4월 1일
댓글: Manikatam Gaddam 2019년 4월 2일
Hello All,
I am trying to randomly add or subtract a constant number to all array elments.
For suppose
a= [1 2 3 4]
b = a + either (0.5 only or -0.5 only)
Is there such possiblity in matlab?

채택된 답변

Image Analyst
Image Analyst 2019년 4월 2일
Try this:
a = [1 2 3 4]
r = randi([0, 1], size(a)) - 0.5
b = a + r

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by