random add or subtract a number to an array

1 次查看(过去 30 天)
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