Function to check whether an array contains a duplicate of elements
15 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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 个评论
Walter Roberson
2023-7-21
Note that this might not behave as expected if there are NaN in the array.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!