random selection of number among 2 numbers

6 次查看(过去 30 天)
How do I randomly choose among 0 and 2 in matlab ? I have display the choosen number also.
Note: The choosen number should be either 0 or 2 not something in between.

回答(1 个)

the cyclist
the cyclist 2021-6-9
编辑:the cyclist 2021-6-9
Here are a couple ways:
2*randi([0 1])
2*round(rand())
  2 个评论
Steven Lord
Steven Lord 2021-6-9
A generalized version of Jan's answer:
Pool = (0:5).^2;
desiredSize = [1 7];
x = Pool(randi(numel(Pool), desiredSize))
x = 1×7
25 16 4 0 25 9 1

请先登录,再进行评论。

类别

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