MATLAB: If all elemets of a array equal, which array will be choose by MAX function

matlab function

Hi
I have an a = [0, 0 , 0 , 0 , 0 ] matrix and when try to use max(a) it always choose the first element as a max value. I need a function like MAX(x) but when all the elements are equal to each other is should choose the one of them randomly. is there any function like that ?

Best Answer

There is no inbuilt function, but you can do it yourself very easily:
>> a = [0,0,0,0,0];
>> ida = find(a==max(a));
>> idx = randi(numel(ida))
idx = 4
>> a(ida(idx))
ans = 0
Related Question