Creating a function that identifies repeated items in a vector

2 ビュー (過去 30 日間)
Sang Yeob Kim
Sang Yeob Kim 2014 年 12 月 4 日
編集済み: Image Analyst 2014 年 12 月 5 日
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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 4 日
What is the expected result?
per isakson
per isakson 2014 年 12 月 4 日
Homework? What you done so far?

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

採用された回答

Mohammad Abouali
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 件のコメント
Sang Yeob Kim
Sang Yeob Kim 2014 年 12 月 5 日
編集済み: Image Analyst 2014 年 12 月 5 日
Based on your answer, I created the the function.
function repeat(v)
result=(numel(v)~=numel(unique(v)))
end
And I ran the program with the following:
>> w = [11 11 22 33 44]
w =
11 11 22 33 44
>> repeat(v)
result =
0
This should give out 1. I don't understand
Image Analyst
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 ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by