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

In summary, the conversation discusses the goal of generating a Poisson distributed vector of random numbers with a non-zero mean, and the code that has been attempted to achieve this. The code provided initially checks each element in the vector and redraws it if it is equal to 0, but this does not guarantee that all elements in the vector will be non-zero. A possible solution suggested is to use a loop that checks each element and redraws the entire vector if any of the elements are equal to 0. However, it is mentioned that this might not completely eliminate the possibility of getting a 0 in the vector.
  • #1
*FaerieLight*
43
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
  • #2
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.
 

1. What are while and for loops in MATLAB?

While and for loops are control flow statements in MATLAB that allow you to execute a block of code repeatedly. They are used for automating repetitive tasks and for iterating through data structures such as arrays and matrices.

2. How do while and for loops differ from each other?

The main difference between while and for loops is that a while loop will continue to execute as long as the specified condition is true, while a for loop will execute a specified number of times, based on a defined range or set of elements.

3. Can you give an example of a while loop in MATLAB?

Yes, here is an example of a while loop that prints numbers from 1 to 10:

num = 1;

while num <= 10

disp(num);

num = num + 1;

end

This loop will continue to execute until the condition "num <= 10" is no longer true, and each time it will print the current value of num.

4. How do you break out of a loop in MATLAB?

To break out of a loop in MATLAB, you can use the "break" statement. This will immediately exit the loop, regardless of the current condition or iteration. Alternatively, you can use "continue" to skip the current iteration and move on to the next one.

5. Are there any performance differences between while and for loops in MATLAB?

In most cases, for loops tend to be slightly faster than while loops in MATLAB. This is because for loops use a counter variable to keep track of the number of iterations, while while loops need to evaluate a condition each time before executing the loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
556
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
739
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top