Randi except one variable

25 次查看(过去 30 天)
Alice Amitrano
Alice Amitrano 2019-3-18
My randi matrix can have random values except one value, how do I generate it?
  1 个评论
Kevin Phung
Kevin Phung 2019-3-18
which value? are you talking about a particular index? and what would that value be?

请先登录,再进行评论。

回答(2 个)

Kevin Phung
Kevin Phung 2019-3-18
I would suggest generating a mxn randi matrix, and just changing the one index to whatever value you wanted it to be.

Walter Roberson
Walter Roberson 2019-3-18
Guessing that you mean that you want to randi() over a range of integer values but that there is a particular value that should never be generated, then:
valid_vals = setdiff(low_value:high_value, values_to_exclude);
result = valid_vals( randi(length(valid_vals), number_of_rows, number_of_columns) );
  2 个评论
Akana Juliet
Akana Juliet 2021-6-18
@Walter Roberson Hi! for "values_to_exclude" how do you include multiple integers?
Walter Roberson
Walter Roberson 2021-6-18
values_to_exclude = [3, 8, -5:-2]
just create a list.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Elementary Math 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by