# random permutation of row vector using recursion

function Y = rndprm3(X);

%RNDPRM3 random permutation of row vector using recursion.
%        RNDPRM(3X) random permutation of the vector X
%        X is a row vector. This function returns a
%        random permutation of the elements of X.
%        This implementation uses recursion.

[m,n]=size(X);
if m > 1
error(‘RNDPRM3 accept as inputs only row vectors’);
end

if n <= 1
Y = X;
else
i = 1 + fix(n*rand);  % select at random the position of
% of an  element of Y
x = X(i);             % selected element of Y
X(i) = [];            % take out x from the X list
Z = rndprm3(X);       % permute remaining elements
% at random
Y = [Z,x];            % construct output vector
end % of if block