MATLAB: I’m supposed make a vector called Vec that contains a 1×9 ranging from integers 1 through 9 in random order. There always tends to be a zero in the vector when the code is ran. How to find the zeros and replace them with an integer from 1-9.

findindexingMATLABvector

%I have this so far
Vec = rand(1,9)*9;
%I keep getting zeros in the vector when the code is ran. How can find the zero and replace with another integer from 1-9.

Best Answer

I don’t get zeros when I run it, but then I don’t get integers. (I suspect you’re using fix or round to get the integers. You need to use ceil:
Vec = ceil(rand(1,9)*9);
Another function you might find useful is randi that by default produces random positive integers (unless you ask it to return random integers over a different range).