Question: I need to find different ways to make a function that generates a vector x of n random but unique elements that are smaller or

I need to find different ways to make a function that generates a vector x of n random but unique elements that are smaller or equal to m. If a number is repeated, the function should return an empty vector. I tried using a for-loop and break statement, but it isn't really working properly. This is my code:

function random=r(m,n)

x=randi(m,1,n);

for i=1:m

if sum(x == i) > 1

break

disp([]);

else

disp(x);

end

end

end

Could somebody tell me what I'm doing wrong? And maybe tell me how I could approach this problem differently too? Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!