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

조회 수: 2 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by