Clear Filters
Clear Filters

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

3 views (last 30 days)
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??

Accepted Answer

Voss
Voss on 23 Feb 2023
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

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!