MATLAB: How to select 2nd largest number (or largest if no 2nd largest)

basic

I need to find the second largest number in a vector but the number of elements from 4 to 1 so sometimes there is no second largest number (only the largest number) and in this case I can use the largest number.
I started with b=sort(a) c=(b(:,end-1)
but obviously I get an error when there is only 1 element.
Sorry if I am missing the obvious here!

Best Answer

Is it possible to have repeated values? If so, what do you define as the "second largest" value? Eg secondmax([1 2 3 3]) = 2? or 3?
y = [x(1),unique(x)];
z = y(end-1);
gives the first option. Replacing unique with sort gives the second.