Code covered by the BSD License  

Highlights from
ISUNIQ

from ISUNIQ by Abhinava Kundu
function isuniq(array)

value=isuniq(array);
function value=isuniq(array);                                               %----------Programmed by: ABHINAVA KUNDU-----------%
% FUNCTION isuniq(aaray)
% Format: value=isuniq(array)
% Checks whether a vector named 'array' has unique/distinct elements or not.
% It returns a value of either '1' or '0' depending upon the nature of the
% vector 'array'. If 'array' has all distinct elements, then the value '1' 
% is returned else a '0' is returned. The vector can be 1 or 2-dimensional.

%Start of checking
value=1;                                                                    %'Value' stores the status of uniqueness; i.e.; it is '1' for TRUE uniqueness else it's '0'
[m n]=size(array);                                                          %The size of the vector 'array'; 'm'-Rows by 'n'-Columns

if(n==1 || m==1)                                                            %If the vector is 1-Dimensional
    if(~isequal((size(array)-size(unique(array))),[0 0]))                   %Comparing the size of the vector with the result of 'unique' function on the vector
        value=0;                                                            %The sizes are different and hence their difference is not [0 0]
    end
end

if(n>1 && m>1)                                                              %If the vector is 2-Dimensional
    for j=1:n,                                                              %For Row-wise checking of uniqueness for each element
        for i=1:m,
            mat=array;                                                      %'mat' is a temporary array on which the operations are carried out
            for k=1:m,
                mat(k,j)=array(i,j);
                if(~isequal((size(mat(k,:))-size(unique(mat(k,:)))),[0 0]))
                    value=0;                                                %If 2 elements are found to be same, value is set to '0'
                    break;
                end
            end
        end
    end
    
    for i=1:n,                                                              %For Column-wise checking of uniqueness for each element after the row checking is done
        if(~isequal((size(array(:,i))-size(unique(array(:,i)))),[0 0]))
           value=0;                                                         %If 2 elements are found to be same, value is set to '0'
           break;
        end
    end
end
%Checking done; Return result

Contact us