How to find location of elements in an array

I have 2 vectors 'A' of size 41*1 and 'B' of size 14*1
vector 'B' contains selected elements from vector 'A'. how do I find the position of elements present in vector 'b' in vector 'a'?
Example: A=1:1:100
B=2:2:20
Now I want to find the position of elements of B in A.
I tried find function but it throws dimention error.
Thanks in Advance

Respuestas (2)

KALYAN ACHARJYA
KALYAN ACHARJYA el 5 de Jul. de 2020
Editada: KALYAN ACHARJYA el 5 de Jul. de 2020
idx=find(ismember(A,B))

5 comentarios

Praanesh Sambath
Praanesh Sambath el 5 de Jul. de 2020
this dosent return the position. just says if it is present or not
As logical indices:
idx = ismember(A,B)
As linear indices:
[~,idy] = ismember(B,A)
KALYAN ACHARJYA
KALYAN ACHARJYA el 5 de Jul. de 2020
Editada: KALYAN ACHARJYA el 5 de Jul. de 2020
Edited see the answer, idx=find(ismember(A,B))
KALYAN ACHARJYA
KALYAN ACHARJYA el 5 de Jul. de 2020
Yes, thank you Stephen Cobeldick sir
Praanesh Sambath
Praanesh Sambath el 5 de Jul. de 2020
Thanks it worked

Iniciar sesión para comentar.

Kanupriya Singh
Kanupriya Singh el 5 de Jul. de 2020
Try this-
a = [2 1 3 4 6 8]
b = [2 6 8]
idx = [];
for i = 1:length(b)
idx = [idx, find(b(i) == a)];
end

2 comentarios

Stephen23
Stephen23 el 5 de Jul. de 2020
Rather complex... one ismember call is much simpler (and is what most MATLAB users would do).
Praanesh Sambath
Praanesh Sambath el 5 de Jul. de 2020
I get this error
error: horizontal dimensions mismatch (0x2 vs 1x1)
>> idx
idx = [](0x2)

Iniciar sesión para comentar.

Categorías

Más información sobre Aerospace Blockset en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 5 de Jul. de 2020

Comentada:

el 5 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by