Creating a function that identifies repeated items in a vector
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sang Yeob Kim
el 4 de Dic. de 2014
Editada: Image Analyst
el 5 de Dic. de 2014
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 comentarios
Respuesta aceptada
Mohammad Abouali
el 4 de Dic. de 2014
Editada: Mohammad Abouali
el 4 de Dic. de 2014
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 comentarios
Sang Yeob Kim
el 5 de Dic. de 2014
Editada: Image Analyst
el 5 de Dic. de 2014
Image Analyst
el 5 de Dic. de 2014
Editada: Image Analyst
el 5 de Dic. de 2014
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Multidimensional Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!