replace zeros according to distribution of integers in each column

if I have some matrix x of randomly distributed values (value 0 to n = 5 or 10 in this case)
x = [binornd( 5, 0.05, 10000, 1 ), binornd( 10, 0.1, 10000, 1 )];
I want to create a second matrix where any zeros in x are replaced at random by a value from 1 to n according to the distribution when zero is excluded in that particular column. I want to do this by random sampling from the values in each column excluding zero.
Any ideas how to do this quickly (in reality the matrices are considerably bigger than this example...)

 Akzeptierte Antwort

Guillaume
Guillaume am 22 Okt. 2016
Bearbeitet: Guillaume am 22 Okt. 2016
If I understand correctly, you want to replace all zeros in a column by random non-zero values from the same column? In which case:
replacementcount = sum(x == 0);
for column = 1:size(x, 2)
validvalues = nonzeros(x(:, column));
x(x(:, column) == 0, column) = validvalues(randperm(numel(validvalues), replacementcount(column)));
end

3 Kommentare

Thanks for the quick reply.
Unfortunately I get an error:
Error using randperm
K must be less than or equal to N.
but I changed it to
replacementcount = sum(x == 0);
for column = 1:size(x, 2)
validvalues = nonzeros(x(:, column));
x(x(:, column) == 0, column) = datasample(validvalues, replacementcount(column), 'Replace', true);
end
and it seemed to work. So thanks!
I wonder if there's an easier way without the need for loops. If I can avoid then I really would like to speed up this code
The error with randperm is because you have more 0s in the column than non-zeros, something I had not considered.
Because the number of 0s can vary per column you cannot avoid the loop. Note that it's only looping over the columns whose number I understood is negligible compared to the number of rows.
Thanks for the reply and help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andriy Kavetsky
Andriy Kavetsky am 22 Okt. 2016

0 Stimmen

You can try this n=10; or n=5; and x(x==0)=randi(n)

Gefragt:

J T
am 22 Okt. 2016

Kommentiert:

J T
am 23 Okt. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by