Function to check whether an array contains a duplicate of elements

15 ビュー (過去 30 日間)
Mina
Mina 2021 年 7 月 11 日
コメント済み: Walter Roberson 2023 年 7 月 21 日
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 件のコメント
the cyclist
the cyclist 2021 年 7 月 11 日
編集済み: the cyclist 2021 年 7 月 11 日
Is this a homework assignment? What have you tried yourself? Maybe we can help your learn MATLAB, rather than do your work for you.
Walter Roberson
Walter Roberson 2021 年 7 月 11 日
Okay, go ahead.

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2021 年 7 月 11 日
編集済み: dpb 2021 年 7 月 12 日
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 件のコメント
dpb
dpb 2021 年 7 月 12 日
Yeah, thanks...
Walter Roberson
Walter Roberson 2023 年 7 月 21 日
Note that this might not behave as expected if there are NaN in the array.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by