First find the null elements, then generate random values, then replace these elements:
Population = [3, 5, 0, 2, 0, 5, 10, 50, 0, 1]; idx = find(Population==0); Population(idx) = 3 * rand(size(idx)) + 1;
If you need integers (do not specify), simply round the generated random numbers in the last statement, for example, this is round(3*rand(size(idx))+1) ; or use randi (as pointed out by @OmG's answer): randi([1,4], size(idx)) .
source share