How to find location of elements in an array

조회 수: 6 (최근 30일)
Praanesh Sambath
Praanesh Sambath 2020년 7월 5일
댓글: Praanesh Sambath 2020년 7월 5일
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

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 7월 5일
편집: KALYAN ACHARJYA 2020년 7월 5일
idx=find(ismember(A,B))
  댓글 수: 5
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 7월 5일
Yes, thank you Stephen Cobeldick sir
Praanesh Sambath
Praanesh Sambath 2020년 7월 5일
Thanks it worked

댓글을 달려면 로그인하십시오.


Kanupriya Singh
Kanupriya Singh 2020년 7월 5일
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
Stephen23
Stephen23 2020년 7월 5일
Rather complex... one ismember call is much simpler (and is what most MATLAB users would do).
Praanesh Sambath
Praanesh Sambath 2020년 7월 5일
I get this error
error: horizontal dimensions mismatch (0x2 vs 1x1)
>> idx
idx = [](0x2)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by