Get every possible combination of an array

4 ビュー (過去 30 日間)
kanav prashar
kanav prashar 2020 年 2 月 26 日
コメント済み: kanav prashar 2020 年 2 月 26 日
I have an array that contains n number of 1s and -1s for example:
[1 -1 1 -1 1 1 1 -1]
I'd like every possible combination of this array (like [1 - 1-1 1 1 1 1 -1] ect...)
I tried using perms, but perms has a limit of size 10 and I need to be able to do upto 32. Another thing about perms is I'll have repeating numbers (which I can remove with ease)
  1 件のコメント
kanav prashar
kanav prashar 2020 年 2 月 26 日
I meant repeating combinations sorry!

サインインしてコメントする。

採用された回答

Benjamin Großmann
Benjamin Großmann 2020 年 2 月 26 日
編集済み: Benjamin Großmann 2020 年 2 月 26 日
This could be a starting point:
clearvars, close all
clc
arr = [-1 1 -1 1 1 1 1 1 -1];
num_arr_elements = numel(arr);
% find the number of -1
num_minus_one = sum(arr == -1);
% get all possible positions of -1
idx = nchoosek(1:num_arr_elements, num_minus_one);
% Initialize the matrix of combinations with ones
out = ones(size(idx,1), num_arr_elements);
% Place the -1s on the idx position
for ii = 1:size(idx,1)
out(ii,idx(ii,:)) = -1;
end
Two more aspects:
  • We have to get rid of this for loop
  • Number of possible combinations is with n: array length, s1: number of 1s and s2: number of -1s; for n = 32 and (worst case) s1=s2=16, we have an output array of size 9 x 601.080.390. Optimization is heavily needed or buy a lot of RAM.
  4 件のコメント
Benjamin Großmann
Benjamin Großmann 2020 年 2 月 26 日
編集済み: Benjamin Großmann 2020 年 2 月 26 日
You are welcome! As Steven said, the brute force method is mostly not optimal to solve a specific task. I am also interested in the underlying problem. Furthermore, I would propose to use a logical array, since you have "bitnumbers" and then solve the task using bit-operations.
kanav prashar
kanav prashar 2020 年 2 月 26 日
I'm doing some simulations on pseudo-random sequences in non-destructive testing, I found that depending on sequence size you get sidelobe images at a certain level (dB) and these sidelobes are based on the "randomness" of these signals (the 1s and -1s). I want to see how changing the size effects the range of these sidelobes, ideally I'd test every possible combination, however realistically thinking about data size now I understand thats not possible. I already know the worst case scenario happens when there is no randomness in the signal i.e. there is a repeating period -1,1,-1,1,-1... etc is one of these bad responses. I want to also identify the best response, I'll probably just stick to a large data set maybe 300 - 400 per codelength and do 1 forced test on the bad cases and hopefully see a trend.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by