How to do permutations with conditions
1 次查看(过去 30 天)
显示 更早的评论
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
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
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
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{:})';
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!