how to find the indices of the numbers in an vector WITHOUT using loops and find function?

5 visualizaciones (últimos 30 días)
for example in an vector a ={100,182,100,487,4,100,44,66,9}
the indices of 100 should be 0 2 5.
  1 comentario
Jan
Jan el 22 de Ag. de 2012
Without loops and FIND?! Tell your teacher that such homework questions help to learn how to use Matlab in the worst way. And the forum will have to cleanup this junk afterwards. Sorry, I cannot resist to solve the "WITHOUT builtin function" homework question. Please be sure, that you mention the forum as assistence, when you provide the solutions to your teacher.

Iniciar sesión para comentar.

Respuestas (4)

Jan
Jan el 22 de Ag. de 2012
Editada: Jan el 22 de Ag. de 2012
a = [100,182,100,487,4,100,44,66,9];
b = 0:length(a) - 1;
result = b(a == 100);
  3 comentarios

Iniciar sesión para comentar.


Jan
Jan el 22 de Ag. de 2012
a = [100,182,100,487,4,100,44,66,9];
result = strfind(a, 100) - 1
  2 comentarios
Oleg Komarov
Oleg Komarov el 22 de Ag. de 2012
Uhm...I read a "find" in your solution, didn't you read the request carefully? He doesn't want to "find" it!
Jan
Jan el 22 de Ag. de 2012
Editada: Jan el 22 de Ag. de 2012
No, Oleg, the name of this command is comming from STRuggle For INDex. And, yes, I know that this name is what we call in German "behumst", because actually you get the INDex Of SUBstringS such that it should be called INDOSUBS. But the strange naming convention has been caused by the former insecure FINDSTR command, which, in fact, meant FIND STRing.
Do you find a REGEXP version?

Iniciar sesión para comentar.


Tomas Jurena
Tomas Jurena el 22 de Ag. de 2012
Hi, your problem could be solved by following function, which takes into account different indexing of elements in an array (in MATLAB, the very first element of an array is indexed by 1, whereas in C programming language it has index 0):
function ind = find_index(A,value,firstElemIndex)
i = firstElemIndex:length(A)+firstElemIndex-1;
v = (A==value);
ind = i(v);
end
Function call for your example:
a=[100,182,100,487,4,100,44,66,9];
ind = find_index(a,100,0);
Tomas

Daniel Shub
Daniel Shub el 23 de Ag. de 2012
Editada: Daniel Shub el 23 de Ag. de 2012
Despite thinking this is a homework assignment, I sometimes like to solve problems with silly constraints. Taking up Jan's challenge for a REGEXP solution gives
a = [100,182,100,487,4,100,44,66,9];
regexp(num2str([a==100]')', '1')-1;
but I think using regexp and strfind are both cheating (even more than asking us to to the homework).
Whenever I hear no loops, I think recursion:
function ii = isx(x, y)
ii = [];
if length(x) > 1
ii = isx(x(2:end), y)+1;
end
if x(1) == y
ii = [1, ii];
end
end
a = [100,182,100,487,4,100,44,66,9];
isx(a, 100)-1

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by