how can I collect all possible combinations of two numbers from a data set in matlab ?

조회 수: 3 (최근 30일)
Dear All,
I have a matrix, like the one s below:
A=[1 2 4 8 16 32];
I want to collect all the possible cases of chossing two array from the above matrix and then have the sum of these values. like below:
1+2=3;
8+32=40;
2+16=18;
How is it possible to access all the possibe combination of two array from a matrix?
I appreciate your support in advance.

채택된 답변

DGM
DGM 2021년 10월 9일
편집: DGM 2021년 10월 9일
Something like this:
A = [1 2 4 8 16 32];
k = 2;
B = nchoosek(A,k)
C = sum(B,2)
Be aware how fast the size of B will increase as the length of A increases. For A of length N, B will have N!/(k!*(N-k)!) rows.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by