Matlab Quick Guide 79: Usage of Functions Related to Permutations and Combinations

I am freexyn, afraid that you won’t know how to write code!

1.Description

In mathematics, the binomial coefficient is defined as the coefficient of (1 + x)ⁿ when expanded, where n is a natural number, and k is an integer. Its value equals the combination number, n!/((n–k)! k!)

2.Understanding Functions

Combinationnchoosek

Permutationsperms

3.Programming Practice for Solving Permutation and Combination Problems

(1) Select any 3 numbers from the four numbers 1, 2, 3, 4 and find all combinations and the number of combinations

(2) Select any 2 letters from the three letters a, b, c and find all combinations and the number of combinations

(3) Find all permutations and the number of permutations of the three numbers 1, 2, 3

(4) Find all permutations and the number of permutations of the three letters a, b, c

The Matlab program code is as follows:

ans1=nchoosek(1:4,3) %Combination

ans1 =

1 2 3

1 2 4

1 3 4

2 3 4

ans2=nchoosek(4,3) %Number of Combinations

ans2 =

4

ans3=nchoosek(‘abc’,2)

ans3 =

3×2 char array

‘ab’

‘ac’

‘bc’

ans4=nchoosek(3,2)

ans4 =

3

ans5=perms(1:3) %Permutations

ans5 =

3 2 1

3 1 2

2 3 1

2 1 3

1 3 2

1 2 3

ans6=perms(‘abc’)

ans6 =

6×3 char array

‘cba’

‘cab’

‘bca’

‘bac’

‘acb’

‘abc’

Related Articles

Reading and Reproducing Papers: Robot Trajectory Parameter Identification | System Dynamics Model | Least Squares Method | Genetic Algorithm Solutions

Latest AI Image Tool: Google Releases Gemini 2.5 Flash Image, reportedly surpassing Photoshop

Algorithm Code Quick Guide 16: A Comprehensive Overview | Essential Classic: Gradient Descent Method, with Matlab Program Examples

Matlab Easy Practice Problem 4.5.1 How to Draw Images Rotating Around the Z-axis

Matlab Quick Guide Basic 77: Summation of Sequences, Applications of Geometric and Arithmetic Sequences

End

Leave a Comment