Dumitru MIhai
Dumitru MIhai

Reputation: 9

Matlab, intersection of two vectors

i want to determinate the positions when 2 vectors are intersected without using repetitive operations. For example

A = [ 2 2 3 4 5]
B = [ 2 3 3 8 5] 

And the output will be

R = [1 3 5].

Upvotes: 1

Views: 223

Answers (1)

Suever
Suever

Reputation: 65460

You can simply use find with a logical matrix:

A = [2 2 3 4 5];
B = [2 3 3 8 5];

R = find(A == B)

    1   3   5

The expression A == B will create a logical matrix where an element is true (1) if the element in A is equal to the element in B and false (0) if they are not equal. Then find will identify the positions in this logical matrix where the values are true.

Upvotes: 4

Related Questions