필터 지우기
필터 지우기

How to find a combination like this

조회 수: 1 (최근 30일)
Nooh Hood Mohamed Almahfoodhi
Nooh Hood Mohamed Almahfoodhi 2019년 2월 24일
댓글: Walter Roberson 2019년 3월 1일
I am wondering is there a way where I could use a matrix in combinatorics such as
nchoosek(4,1:4) where it calcluates 4choose1, 4choose2,... 4choose4 ?
Is thie possible in Matlab?

채택된 답변

Walter Roberson
Walter Roberson 2019년 2월 24일
N = 4;
temp = factorial(0:N);
temp(end) ./ (temp .* fliplr(temp))
This would be 4C0, 4C1, 4C2, 4C3, 4C4, so you could index into the result to get the subset you need.
  댓글 수: 2
Nooh Hood Mohamed Almahfoodhi
Nooh Hood Mohamed Almahfoodhi 2019년 3월 1일
Thank you that worked.
Walter Roberson
Walter Roberson 2019년 3월 1일
temp = cumprod([1 1:N]);
turns out to be faster than factorial(0:N)

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2019년 3월 1일
Computationally less efficient, and mathematically less beautiful as Walters' snippet, but more matlab-ish in style:
N = 4 ;
C = arrayfun(@(k) nchoosek(N, k), 0:N)
or after same mathematical adjustments to N! / K!(N-K)!
C2 = arrayfun(@(k) prod(k+1:N)/prod(2:N-k), 0:N)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by