필터 지우기
필터 지우기

How to do permutations with conditions

조회 수: 3 (최근 30일)
John Doe
John Doe 2020년 3월 23일
댓글: John Doe 2020년 3월 24일
Hello everyone,
I have the following to get permutations
V= ([1:0.5:5]');
Comb = permn(V,2);
Now, I still want to permutate but I don't want to have permutations that start with 1 but end with it.
So my results now:
1 1
1 1.5
1 2
1 2.5
1 3
1 3.5
1 4
1 4.5
1 5
1.5 1
1.5 1.5
1.5 2
1.5 2.5
1.5 3
.
.
.
I still want the same results but I don't want the results that start with 1 in the first column but I still want 1 to be present in Comb but in my second column. I know I can simply use this:
find(AllCombinations(:,1) ==1 )
Then delete what I don't want, but I was wondering if there's a way to set it as a condition as I permutate, so just one step.
Thank you, if need more clarifications please let me know.
  댓글 수: 1
Sindar
Sindar 2020년 3월 24일
I doubt there is a way to do this from the start, but there is a slightly better way to delete:
V= ([1:0.5:5]');
Comb = permn(V,2);
Comb( Comb(:,1)==1,:) = [];

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

채택된 답변

Sindar
Sindar 2020년 3월 24일
Actually, this should work
V= (1:0.5:5);
Comb = combvec(V(2:end),V)';
% if you want sorted by first column:
Comb = sortrows(Comb,1);
  댓글 수: 3
Sindar
Sindar 2020년 3월 24일
With these methods, you need to repeat the arguments as many times as you need. Luckily, this stuff scales so fast that you probably won't reach the point where this is too bad:
% 5 elements
Comb = allcomb(V(2:end),V,V,V,V)';
If you need to programmatically change the number of elements, you can probably do it with a cell array and argument expansion:
N = 5;
V_s = {};
for ind=1:(N-1)
V_s{ind} = V;
end
Comb = allcomb(V(2:end),V_s{:})';
John Doe
John Doe 2020년 3월 24일
Thank you so much!! This is great!

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by