Permutations of an array by fixing some element

Hello,
I need to permute the elements of an array but some specific elements have to be fixed. For example, consider the following array.
array = [1 0 3 0 5 0]
All the 0's in this array must be fixed. Other elements can change their positions, For this example, the following are all the possible results.
1 0 3 0 5 0
1 0 5 0 3 0
3 0 1 0 5 0
3 0 5 0 1 0
5 0 1 0 3 0
5 0 3 0 1 0
Also, I need a general algorithm that works for any given 1xn array, and I need all the possible results.
Thanks for your efforts.

 채택된 답변

Bruno Luong
Bruno Luong 2020년 8월 21일

0 개 추천

array=[1 0 3 0 5 0]
b = array~=0;
A = zeros(factorial(sum(b)),length(array));
A(:,b) = perms(array(b))

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2020년 8월 21일

댓글:

2020년 8월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by