Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB vectorization to create a matrix

I want to create a matrix like

[1 2;
 1 3;
 1 4;
 1 5;
 2 3;
 2 4;
 2 5;
 3 4;
 3 5;
 4 5 ]

when the size is 5. I aim to have sizes greater than 100. How can I create a matrix like this using vertorization in MATLAB?

like image 339
Chang Avatar asked Sep 02 '26 08:09

Chang


1 Answers

You're looking for binomial coefficients, so use the built-in nchoosek command. For example, the matrix in your question can be generated by:

A = nchoosek(1:5, 2)

This results in:

A =
     1     2
     1     3
     1     4
     1     5
     2     3
     2     4
     2     5
     3     4
     3     5
     4     5
like image 103
Eitan T Avatar answered Sep 04 '26 23:09

Eitan T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!