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

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by