# Finding the elements in two matrices with different sizes?

18 visualizzazioni (ultimi 30 giorni)
Cladio Andrea il 24 Feb 2015
Risposto: Cladio Andrea il 24 Feb 2015
Hello eveeryone, i have 2 matrices lets say A=[1,2,3,4,5,6,7,8] B = [1,11,12,3,3,5] what i want is to have such a result C=[1,3,3,5] what i want is compare two matrices ok that we can do that with intersect(), but in intersect function i only have [1,3,5], that is not what i want. what i want is even if it is repeated in matrix B compare with A and then have a matrix of C. The A and B sizes changing for each simulation. Can you help me please?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Cladio Andrea il 24 Feb 2015
and in matrix A there is no possiblity to have repeating elements, all different.

Accedi per commentare.

### Risposta accettata

Roger Stafford il 24 Feb 2015
You can also use 'ismember':
C = B(ismember(B,A));
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (2)

Image Analyst il 24 Feb 2015
Try setdiff():
A=[1,2,3,4,5,6,7,8]
B = [1,11,12,3,3,5]
[~, ia]=setdiff(B, A)
C = B; % Initialize to B
C(ia) = [] % Remove different elements.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Cladio Andrea il 24 Feb 2015
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by