How o generate random integer value?

Example:
I have used
A=randi([1 10],1,12)
output:
A=[ 1 9 1 6 7 8 3 7 9 7 8 5]
Is it possible to generate random integer values in interval [1 10], excluding 3, 6 and 8 from the interval having said that the size of the array 1 by 12 remains same?

 採用された回答

John D'Errico
John D'Errico 2020 年 7 月 15 日
編集済み: John D'Errico 2020 年 7 月 15 日

0 投票

You want to allow the numbers [1 2 4 5 7 9 10]. So just generate a random integer from 1 to 7. Then use that vector to index the allowed set.
allowed = [1 2 4 5 7 9 10];
A = allowed(randi(7,[1,12]));
If you have some more difficult set to allow, then use setdiff.
allowed = setdiff(1:10,[3 6 8]);

1 件のコメント

SM
SM 2020 年 7 月 16 日
First option is more suitable for my problem. Thank you John!

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 7 月 15 日

0 投票

a = 1:10;
No_vals = [3, 6, 8];
A = setdiff(a, No_vals);
Wanted = A(randi(numel(A), [1, 12]))

1 件のコメント

SM
SM 2020 年 7 月 16 日
Thank you Ravi....Your solution is more comprehensive and easy to understand.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLinear Algebra についてさらに検索

タグ

質問済み:

SM
2020 年 7 月 15 日

コメント済み:

SM
2020 年 7 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by