# Q：给MATLAB中的两个数之间的所有整数

Need some hints, how do i write a function called int_col that has one input argument, a positive integer n that is greater than 1, and one output argument v that is a column vector of length n containing all the positive integers smaller than or equal to n, arranged in such a way that no element of the vector equals its own index. In other words, v(k) is not equal to k for any valid index k.

i used this code

``````function v = int_col(n)
v = [n:-1:1]'
end
``````

but the solver is giving me error for input argument 3

``````function v = int_col(n)
v = [n:-1:1]'
end
``````

Her is a solution using loops:

``````function v = int_col(n)

v = zeros(1,n);
for index=1:n
val = randi(n);
while (val == index || any(val == v))
val = randi(n);
if (index == n && (any(v == n) ~= 1))
val = n;
break
end
end
v(index) = val;
if (index == n && val == n)
v(n) = v(n-1);
v(n-1) = n;
end
end
v = v';
end
``````

``````function v = int_col(n)

v = zeros(1,n);
for index=1:n
val = randi(n);
while (val == index || any(val == v))
val = randi(n);
if (index == n && (any(v == n) ~= 1))
val = n;
break
end
end
v(index) = val;
if (index == n && val == n)
v(n) = v(n-1);
v(n-1) = n;
end
end
v = v';
end
``````
``````function[v]=int_col(n)
v=zeros(n,1);
k=1:n;
p=cat(2,k(n),k(1:n-1));
v=p'
end
``````
``````function[v]=int_col(n)
v=zeros(n,1);
k=1:n;
p=cat(2,k(n),k(1:n-1));
v=p'
end
``````
matlab  random  numbers  integer