フィルターのクリア

How to change a range of numbers in a matrix to a random number of a matrix?

3 ビュー (過去 30 日間)
Sumanth
Sumanth 2023 年 2 月 23 日
コメント済み: Voss 2023 年 2 月 27 日
G = (1:125);
G = reshape(1:125,[25,5]);
x = setdiff(0:24, 2:25);
G is my matrix now i want to change element values ranging from 2 to 25 to random number from x
How can i do it??

採用された回答

Voss
Voss 2023 年 2 月 23 日
G = reshape(1:125,[25,5])
G = 25×5
1 26 51 76 101 2 27 52 77 102 3 28 53 78 103 4 29 54 79 104 5 30 55 80 105 6 31 56 81 106 7 32 57 82 107 8 33 58 83 108 9 34 59 84 109 10 35 60 85 110
x = setdiff(0:24, 2:25)
x = 1×2
0 1
idx = G >= 2 & G <= 25;
G(idx) = x(randi(numel(x),[nnz(idx),1]))
G = 25×5
1 26 51 76 101 0 27 52 77 102 1 28 53 78 103 1 29 54 79 104 1 30 55 80 105 1 31 56 81 106 0 32 57 82 107 1 33 58 83 108 1 34 59 84 109 0 35 60 85 110
  2 件のコメント
Sumanth
Sumanth 2023 年 2 月 27 日
Thank you so much!
Voss
Voss 2023 年 2 月 27 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by