I'm supposed make a vector called Vec that contains a 1x9 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 can I find the zeros and replace them with an integer from 1-9.

1 view (last 30 days)
%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.

Accepted Answer

Star Strider
Star Strider on 14 Sep 2016
Edited: Star Strider on 14 Sep 2016
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).

More Answers (0)

Community Treasure Hunt

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

Start Hunting!