Creating a function that identifies repeated items in a vector
조회 수: 1 (최근 30일)
이전 댓글 표시
I need help with creating this repeat function.
Write a function, repeat, that takes as input a vector of arbitrary length whose elements appear in random order. Determine whether the vector contains any repeated items. If it does, return true (1). Otherwise, return false(0). Test it in a program on the following vector: 11 22 33 44 55 66 77 99 11 102
댓글 수: 2
채택된 답변
Mohammad Abouali
2014년 12월 4일
편집: Mohammad Abouali
2014년 12월 4일
testVector=[11 22 33 44 55 66 77 99 11 102];
result=(numel(testVector)~=numel(unique(testVector)))
if testVector has repeated item results would be true; otherwise it would be false.
댓글 수: 2
Image Analyst
2014년 12월 5일
편집: Image Analyst
2014년 12월 5일
You forgot to pass anything back! You need to pass "result" back out:
function result = repeat(v)
result = (numel(v) ~= numel(unique(v)));
end
Please mark the Answer as accepted if that works.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!