Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
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??

Respuesta aceptada

Voss
Voss el 23 de Feb. de 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

Más respuestas (0)

Categorías

Más información sobre Random Number Generation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by