How to do permutations with conditions

2 vues (au cours des 30 derniers jours)
John Doe
John Doe le 23 Mar 2020
Commenté : John Doe le 24 Mar 2020
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 commentaire
Sindar
Sindar le 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,:) = [];

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by