How to do permutations with conditions

1 view (last 30 days)
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 Comment
Sindar
Sindar on 24 Mar 2020
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,:) = [];

Sign in to comment.

Accepted Answer

Sindar
Sindar on 24 Mar 2020
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 Comments
Sindar
Sindar on 24 Mar 2020
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 on 24 Mar 2020
Thank you so much!! This is great!

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!