MATLAB Answers

All possible pairs in a vector

92 views (last 30 days)
Hi, and thank you in advance for taking the time to read this. Is there a quick way to get all possible pairs from a vector? For example A=[1 2 3 4 5]; [1 2], [1 3], [1 4], [1 5], [2 3], [2 4], [2 5], [3 4], [3 5], [4 5]. I think I can get that with 2 for loops but I would like to know if there's a know faction for that.
Thank you

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Apr 2018
Try this:
A = [1 2 3 4 5];
Out = nchoosek(A, 2)
Out =
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

  2 Comments

Antonis Asiminas
Antonis Asiminas on 15 Apr 2018
Works like a charm! Thank you!
Star Strider
Star Strider on 15 Apr 2018
As always, my pleasure!

Sign in to comment.

More Answers (1)

David Fletcher
David Fletcher on 15 Apr 2018
nchoosek(A,2)

  0 Comments

Sign in to comment.

Sign in to answer this question.