Permutations Write a function permutation that will receive a positive integer n
ID: 3693344 • Letter: P
Question
Permutations Write a function permutation that will receive a positive integer n as an input argument. The function will return a vector of size n, where the elements of the vector are the positive integers from 1 to n, in random order. Each number from 1 to n should appear only once. You are required to use the programming method (loops, conditional statements). You are NOT allowed to use the following built-in Matlab functions: perms, randperm, randsample. Here is an example of calling the function:Explanation / Answer
Matlab Function:
function p=permutation(n)
p=zeros(n,1);
for i=1:n
p(i)=i;
end
for i=1:n*n
x=randi([1,n]);
y=randi([1,n]);
z=p(x);
p(x)=p(y);
p(y)=z;
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.