Get n numbers from a list

5 ビュー (過去 30 日間)
loukas
loukas 2022 年 4 月 17 日
回答済み: Dyuman Joshi 2022 年 4 月 17 日
I know there is a function nchoosek, however is there any way to do this without using this particular command since we aren't allowed to use it yet?
For example, [1,2,3,4] and we need to take 3 numbers at time. The results should be [1 2 3] [1 2 4] [1 3 4] [2 3 4]

採用された回答

Dyuman Joshi
Dyuman Joshi 2022 年 4 月 17 日
Binary approach (however, it might get slow for large numbers of element in x)
%vectorized method of this approach might be a little faster
x=[1 2 3 4];
y=[];
for i=1:2^numel(x)-1
if nnz(dec2bin(i,numel(x))=='1')==3
y=[x(dec2bin(i,numel(x))=='1'); y];
end
end
y
y = 4×3
1 2 3 1 2 4 1 3 4 2 3 4

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by