All possible pairs in a vector

조회 수: 33 (최근 30일)
Antonis Asiminas
Antonis Asiminas 2018년 4월 15일
댓글: Star Strider 2018년 4월 15일
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

채택된 답변

Star Strider
Star Strider 2018년 4월 15일
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
Antonis Asiminas
Antonis Asiminas 2018년 4월 15일
Works like a charm! Thank you!
Star Strider
Star Strider 2018년 4월 15일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Fletcher
David Fletcher 2018년 4월 15일

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by