From a57cdb352c40e650a0a4cfa31cb4f6c6b9ceec67 Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Mon, 26 Oct 2020 17:02:17 +1100 Subject: [PATCH] feat(Util): allrepeats, inclusive. Inclusive find of all repetition indexes in an array. --- Util/allrepeats.m | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Util/allrepeats.m diff --git a/Util/allrepeats.m b/Util/allrepeats.m new file mode 100644 index 000000000..0b48344ba --- /dev/null +++ b/Util/allrepeats.m @@ -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