Skip to content

Commit

Permalink
feat(Util): allrepeats, inclusive.
Browse files Browse the repository at this point in the history
Inclusive find of all repetition indexes in an array.
  • Loading branch information
ocehugo committed Nov 2, 2020
1 parent 52cfa2d commit a57cdb3
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Util/allrepeats.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function [bind] = allrepeats(array)
% function [] = allrepeats(array)
%
% Inclusive find of all repeated
% values within an array.
%
% Inputs:
%
% array - the array
%
% Outputs:
%
% bind - indexes where of repeated
% values, including the first
% item.
%
% Example:
%
% %basic usage
% bind = allrepeats([1,2,3,1,5,1]);
% assert(any(bind));
% assert(isequal(bind,[1,4,6]));
%
% %no repeats
% assert(~any(allrepeats([1,2,3])))
%
% author: hugo.oliveira@utas.edu.au
%
if ~isnumeric(array)
error('%s: first argument is not a numeric array',mfilename);
end
bind = [];
[uniq,~,uind] = isunique(array);
if ~uniq
aind = 1:numel(array);
repeats = array(setdiff(aind,uind));
bind = find(ismember(array,repeats));
end

0 comments on commit a57cdb3

Please sign in to comment.