How to compare elements of a 2D array with other elements of the same array.

조회 수: 11 (최근 30일)
Sahil Islam
Sahil Islam 2022년 3월 8일
답변: Arif Hoq 2022년 3월 8일
I have an 2D array where I have to compare each element with all the others and save the index position if the two elements are equal.
How can I do that using lesser for loops?
I can't give the code for array here because it is a big data. Can you answer with an example.
Thanks .
Edit: Initial question might be unclear.
Just to give an example, suppose I have an array A:
A=[1 2 3 ; 2 9 5; 4 1 8]
Here A(1,1) and A(3,2) are both =1
I need to detect all of such pairs or collection of index(if more than two).

답변 (2개)

KSSV
KSSV 2022년 3월 8일
You can use ismember. Read about the function ismember.

Arif Hoq
Arif Hoq 2022년 3월 8일
A=[1 2 3 ; 2 9 5; 4 1 8]
A = 3×3
1 2 3 2 9 5 4 1 8
[A1 C]=ismember(A(:),A);
A2=sort(C); % sorting index
A3=A(A2) % value of A those are identical
A3 = 9×1
1 1 2 2 4 9 3 5 8

카테고리

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

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by