MATLAB: In a sorted vector ,how to compare first element to other elements and then find if there are any duplicates , if yes take their indexes and replace it with 0

syntax

A = [ 1, 1 , 2, 3, 4]
I want to first compare first element to all other elements, if there is any duplicate (like 1, 1 in this example) I need to replace it with 0.

Best Answer

For a sorted row vector. Find duplicates of the first element, as you requested:
>> A = [1,1,2,3,4];
>> X = A(1)==A;
>> X(1) = false;
>> A(X) = 0
A =
1 0 2 3 4
Or perhaps you meant to find all duplicates, not just of the first element:
>> A = [1,1,2,3,4];
>> X = [false,diff(A)==0];
>> A(X) = 0
A =
1 0 2 3 4