Compare 2 arrays with find()

7 views (last 30 days)
Rafael Luque
Rafael Luque on 5 Aug 2019
Commented: Star Strider on 5 Aug 2019
I have 2 arrays:
x = 1:1:3;
Points = [1 3 2 1 1 3 4];
I would like to use find() function comparing each element of 'Points' array with full 'x' array.
It would be similar to do:
find(Points(1) == x)
find(Points(2) == x)
... one by one, but I would like to do it at once without looping. However I get error if I do:
find(Points == x)

Accepted Answer

Star Strider
Star Strider on 5 Aug 2019
The ismember function may do what you want:
x = 1:1:3;
Points = [1 3 2 1 1 3 4];
[~,Out] = ismember(Points, x)
producing:
Out =
1 3 2 1 1 3 0
Experiment with it to get the result you want.
  2 Comments
Rafael Luque
Rafael Luque on 5 Aug 2019
Perfect! Thank you
Star Strider
Star Strider on 5 Aug 2019
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!