フィルターのクリア

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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by