Search Program on this blog

Tuesday 18 August 2015

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.

function v = int_col(n)
%Input n>1 an integer
%Output v is a column vector of elements less than and equal to n without
%same index as element value.
%Example:
%    v=int_col(10)
if n>1 %check if n is greater than 1 or not
    v = [2 : 1 : n 1]'; %values from 2 to n and 1 in last
end

No comments:

Post a Comment