# intersect(A,B) returns the data with no repetitions

4 visualizzazioni (ultimi 30 giorni)
want2know il 16 Ott 2013
Commentato: want2know il 21 Ott 2013
Dear Sir/Madam,
I was using "intersect" in my Matlab code where I want the following:
A = [ 4 1 1 2 3];
[B] = sort(A, 'ascend'); % so that B is sorting A in ascending order, so I got B = [1 1 2 3 4]
[same,a] = intersect(B,A);
I want same = [1 1 2 3 4] but the simulation gives me same = [1 2 3 4] by omitting the repeated '1'.
I understand by using intersect it will return data with no repetition
C = intersect(A,B) returns the data common to both A and B with no repetitions.
I want it to show the complete data including those repetition, what are the alternatives I can use rather than the function "intersect"?
For example:
A = [ 4 1 1 2 3];
[B] = sort(A, 'ascend'); % so that B is sorting A in ascending order, so I got B = [1 1 2 3 4]
[same,a] = intersect(B,A);
So now I want it to be like this same =[1 1 2 3 4] and a=[2 3 4 5 1].
I need to access ‘a’ where ‘a’ shows the original index prior to sorting so I can use it for further processing.
Thank you very much.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Jos (10584) il 16 Ott 2013
I think you are looking for the second output of SORT
A = [ 4 1 1 2 3]
[B, ix] = sort(A, 'ascend')
Now, B equals A(ix)
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
want2know il 21 Ott 2013
Thanks a lot, it is working now, you are absolutely right, sorry for making such silly mistake.

Accedi per commentare.

### Più risposte (2)

Laurent il 16 Ott 2013
You can use the function 'ismember' for this purpose.
From the help:
LIA = ismember(A,B) for arrays A and B returns an array of the same
size as A containing true where the elements of A are in B and false
otherwise.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
want2know il 16 Ott 2013
Modificato: want2know il 16 Ott 2013
Dear Laurent,
Thanks so much for your reply. Sorry that I should have made it clearer: The below shows details:
A = [ 4 1 1 2 3];
[B] = sort(A, 'ascend'); % so that B is sorting A in ascending order, so I got B = [1 1 2 3 4]
[same,a] = intersect(B,A);
So now I want it to be like this same =[1 1 2 3 4] and a=[2 3 4 5 1].
I need to access ‘a’ where ‘a’ shows the original index prior to sorting so I can use it for further processing.
I hope I make myself clear this time, thank you very much.

Accedi per commentare.

Jos (10584) il 16 Ott 2013
I do not get it. Will same not always be exactly B?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
want2know il 16 Ott 2013
Sorry Jos, I should have made it clearer, please refer to my edited question, thanks for your time

Accedi per commentare.

### Categorie

Scopri di più su Loops and Conditional Statements 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