How Can I Generate a Poisson Distributed Vector Without Zeros in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter *FaerieLight*
  • Start date Start date
  • Tags Tags
    For loops Loops Matlab
Click For Summary
SUMMARY

This discussion addresses the generation of a Poisson distributed vector in MATLAB without any zeros. The user initially attempts to use the poissrnd function to create a vector of random numbers but encounters issues with zeros appearing in the output. A suggested solution involves modifying the loop to redraw the entire vector if any element equals zero, ensuring that all elements are non-zero. The revised code effectively resets the index to 1 upon finding a zero, thus rechecking the entire vector.

PREREQUISITES
  • Understanding of Poisson distribution and its properties
  • Familiarity with MATLAB programming and syntax
  • Knowledge of the poissrnd function in MATLAB
  • Basic concepts of loops and conditional statements in programming
NEXT STEPS
  • Explore advanced techniques for generating non-zero random distributions in MATLAB
  • Learn about the implications of the Poisson distribution's mean on generated values
  • Investigate alternative random number generation methods in MATLAB
  • Study error handling and optimization in MATLAB loops
USEFUL FOR

Mathematical programmers, data scientists, and researchers needing to generate non-zero Poisson distributed random numbers in MATLAB.

*FaerieLight*
Messages
43
Reaction score
0
I want to generate a Poisson distributed vector of random numbers, without any of the numbers being 0. The code I have is

k = poissrnd(kmean,1,N);
% where kmean is the mean of the distribution, and has been defined previously
%The above generates a N by 1 vector of Poisson distributed random numbers, with mean and variance kmean. N has also been defined previously.
%poissrnd is a command in newer versions of MATLAB

for i = 1:N
while k(i) == 0
k = poissrnd(kmean,1,N);
end
end

So basically if the vector of random numbers contains a 0, I want to redraw the vector.
The code doesn't work, and I can't seem to make it work. Can someone please give me some advice?

Thanks a lot!
 
Physics news on Phys.org
What your code does is this: it checks each element in k, and if k[i] is 0, it continues to redraw k till k[i] is no longer 0. But how does k[i] ≠ 0 guarantee that k[i-1] ≠ 0? In other words, even though k[i] ≠ 0, some other element in k might have become 0. So you will have to recheck from the beginning. A better code is this:
Matlab:
for i = 1:N
    if k(i) == 0
        k = poissrnd(kmean,1,N);
        i = 1;
    end
end
Note that based on your value of kmean and the algorithm that Matlab uses, you might continue getting a 0 somewhere or the other. I am not sure of this, but it could, in principle, be possible.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
4K