ind() function or find() function not working

At first this section of code was working, and return the right indexes, but now its not working. It should be returning: ind = 3 4 but it returns ind = 1
A = [1 1 2 2 3 4 5 6 7 8 9];
a = length(A);
b = A(3)
ind = find(b)
if length(ind) == 1
bmatch = 0
elseif length(ind) > 1
bmatch = 1
else
end
It returns:
b =
2
ind =
1
bmatch =
0
Any help is greatly appreciated!

3 comentarios

Stephen23
Stephen23 el 7 de Abr. de 2022
Editada: Stephen23 el 7 de Abr. de 2022
"ind() function or find() function not working"
FIND is working correctly.
You use indexing to select one element of A. Then you use FIND on that one nonzero element which correctly returns the linear index 1. Then because the index is scalar the IF evaluates, giving BMATCH == 0.
So far you have not explained why you think the code should do anything other than what it is doing.
"It should be returning: ind = 3 4 "
Nope, not that code.
Katherine Sadler
Katherine Sadler el 7 de Abr. de 2022
Oh I thought the FIND would return the index spot of 3 and 4. Thanks
Stephen23
Stephen23 el 7 de Abr. de 2022
All FIND knows about is the scalar input that you have provided it. FIND cannot guess that that scalar array was origianally in position 3 of a larger array, nor can it guess that there were other identical values in that larger array. It only knows about the scalar element that you gave it.

Iniciar sesión para comentar.

 Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 7 de Abr. de 2022
Use
ind = find(A==b);
%find(x) is generally used for matrices to find non-zero values.

Más respuestas (0)

Categorías

Productos

Versión

R2022a

Etiquetas

Preguntada:

el 7 de Abr. de 2022

Comentada:

el 7 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by