Function to check whether an array contains a duplicate of elements

27 Ansichten (letzte 30 Tage)
Mina
Mina am 11 Jul. 2021
Kommentiert: Walter Roberson am 21 Jul. 2023
I want to write a function that
- Takes as input an array, the value being checked and the starting element of the array where checking for a duplicate should start
- Returns a logical value to indicate whether the array contains a duplicate of the value being checked (stops checking when one is found)
  2 Kommentare
the cyclist
the cyclist am 11 Jul. 2021
Bearbeitet: the cyclist am 11 Jul. 2021
Is this a homework assignment? What have you tried yourself? Maybe we can help your learn MATLAB, rather than do your work for you.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 11 Jul. 2021
Bearbeitet: dpb am 12 Jul. 2021
function flag=isduplicate(x,v,ix)
flag=numel(find(x(ix:end)==v))>1;
end
x is vector or if x is array in linear addressing order.
Otherwise, need indexing expression of row, colum, ... for as many dimensions as are greater > 1 in size(x)
If x is floating point, then for robustness, consider ismembertol instead of exact equality or write a tolerance expression.
  3 Kommentare
Walter Roberson
Walter Roberson am 21 Jul. 2023
Note that this might not behave as expected if there are NaN in the array.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by