Replacing Set Number of Random rows with a value
Show older comments
I have a 1-D numeric matrix (10000x1 double). I would like to select a random 8000 of the entries and replace them with the value "i" while still maintaning the same order.
For instance, if the matrix only had 10 values [1, 5, 8, 3, 7, 3, 7, 2, 4, 3] and I want to replace 8 out of 10 randomly with the number 13, so that the result would be something like [13, 13, 13, 13, 7, 13, 13, 13, 13, 3] or [1, 13, 8, 13, 13, 13, 13, 13, 13, 13]. How would I go about doing this?
Answers (1)
James Tursa
on 12 Nov 2020
0 votes
Use randperm( ) to generate the random indexes, and then just use these indexes in an assignment.
2 Comments
no program
on 12 Nov 2020
James Tursa
on 12 Nov 2020
Just copy matrix first. E.g.,
new_matrix = matrix;
idx = randperm(10000,2000);
new_matrix(idx) = 13;
Categories
Find more on Logical 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!