MATLAB: Choose elements, matrix with fewer elements

matrixrandomsize;

Hi Everyone,
I have two matrices of unknown size and I wish to choose all the elements of the smaller matrix and 'n' random elements from the larger matrix, where 'n' is the length of the smaller matrix. I also do not want to reuse indices when doing the random selection. Could you please teach me how to do this most efficiently?
Thank you!

Best Answer

function y = myFun(a,b)
aN = numel(a);
bN = numel(b);
% Make sure a is smaller
if aN>bN
temp = b;
b = a;
a = temp;
temp = bN;
bN = aN;
aN = temp;
end
i = randperm(bN)';
y = [a(:);b(i(1:aN))];
end