How to find same unique in two different size arrays?

14 Ansichten (letzte 30 Tage)
Ilya Dikariev
Ilya Dikariev am 1 Jun. 2022
Kommentiert: Ilya Dikariev am 1 Jun. 2022
Assuming we have two different array sizes with similar items in it. How to find number of common items in these arrays?
for ex:
x=[1 22 22
3 8 19];
y=[22 10 19 35];
here answer must be 2, reffering to 22 and 19.
Any good ideas? And also, would that be possible to do it without using loop since actual data size is relatively big?

Akzeptierte Antwort

Fabio Freschi
Fabio Freschi am 1 Jun. 2022
Use ismember
% data
x=[1 22 22
3 8 19];
y=[22 10 19 35];
% engine
[iii,jjj] = ismember(y(:),x(:));
countCommon = sum(iii)
entryCommon = y(iii)

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by