Function to check whether an array contains a duplicate of elements
이전 댓글 표시
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.
Walter Roberson
2021년 7월 11일
Okay, go ahead.
답변 (1개)
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
the cyclist
2021년 7월 11일
I think you meant to assign the value to the output variable flag.
dpb
2021년 7월 12일
Yeah, thanks...
Walter Roberson
2023년 7월 21일
Note that this might not behave as expected if there are NaN in the array.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!