to the list of examples

# The feature selection: how to search all combinations?

Below there are several peculiar counters, used in various algorithms on regression modeling

## Count the features

The primary usage of the counter 'cntfeatures' is the feature selection for regression and pattern recognition algorithms. It counts unique numbers from 1 to modulio. Each number is an element in a vector. After overflow the counter resets to zeros. Note that length(cnt) must be equal to modulio.

cnt [1 x modulio] - the prevois value of the counter ib the argument and the next one in the output i [scalar] - dummy variable, must be equal to modulio modulio [scalar] - base modulio to count up.

```modulio = 5;              % Count unques from 1 to 12345, base 5
cnt=zeros(1,modulio);     % Prepare the counter's vector
i=0;                      % Just a variable for the demo purpose
while 1==1                % The loop to count up
i = i+1;               % Increase the demo variable
cnt = cntfeatures(cnt,modulio,modulio);  % Count up
disp(cnt);                               % Show the counter value
if all(cnt == [1:modulio]), break; end   % Check for overflow
end                       % The loop end
disp(['Variants: ',num2str(i)]);                  % Show the value of the demo variable
```
```     0     0     0     0     1

0     0     0     0     2

0     0     0     0     3

0     0     0     0     4

0     0     0     0     5

0     0     0     1     2

0     0     0     1     3

0     0     0     1     4

0     0     0     1     5

0     0     0     2     3

0     0     0     2     4

0     0     0     2     5

0     0     0     3     4

0     0     0     3     5

0     0     0     4     5

0     0     1     2     3

0     0     1     2     4

0     0     1     2     5

0     0     1     3     4

0     0     1     3     5

0     0     1     4     5

0     0     2     3     4

0     0     2     3     5

0     0     2     4     5

0     0     3     4     5

0     1     2     3     4

0     1     2     3     5

0     1     2     4     5

0     1     3     4     5

0     2     3     4     5

1     2     3     4     5

Variants: 31
```

## Count 2^n combinations of elements in a set

Return vector c plus 1 in the last element, up to the value of b. When overflow in the last element, drop it down to the value of a. When i=overflow in all elements, drop all elements to the values of a.

```disp('Here indexes of the features are in the row');
c = zeros(1,4);
for i=1:17
c = cntabover(c,0,1);
disp(c);
end
```
```Here indexes of the features are in the row
0     0     0     1

0     0     1     0

0     0     1     1

0     1     0     0

0     1     0     1

0     1     1     0

0     1     1     1

1     0     0     0

1     0     0     1

1     0     1     0

1     0     1     1

1     1     0     0

1     1     0     1

1     1     1     0

1     1     1     1

0     0     0     0

0     0     0     1

```

## Count 2^n combinations with reinsertion for n=1,2,...

Returns vector c plus 1 in the last element, up to the value of b. When overflow in the last element, drop it down to the value of a. When i=overflow in all elements, it appends the 1st element to the vector c. Its value is a;

```disp('Here index is the number of a feature');
c = 0;
for i=1:17
c = cntabappend(c,1,3);
disp(c);
end
```
```Here index is the number of a feature
1

2

3

1     1

1     2

1     3

2     1

2     2

2     3

3     1

3     2

3     3

1     1     1

1     1     2

1     1     3

1     2     1

1     2     2

```

## Also see Matlab combinatorics

The problem is: useful functions are in toolboxes, not in the Matlab kernel combntns (Mapping toolbox) All possible combinations of set of values, recursive function

```combos = combntns(1:5,3)

% combnk (Statistics toolbox) Enumeration of combinations
```
```combos =

1     2     3
1     2     4
1     2     5
1     3     4
1     3     5
1     4     5
2     3     4
2     3     5
2     4     5
3     4     5

```
```c = combnk(1:4,2)

%combvec (Neural network toolbox) Create all combinations of vectors
a1 = [1 2 3; 4 5 6];
a2 = [7 8; 9 10];
```
```c =

3     4
2     4
2     3
1     4
1     3
1     2

```
```a3 = combvec(a1,a2)

% perms (Statistics toolbox) Enumeration of permutations
```
```a3 =

1     2     3     1     2     3
4     5     6     4     5     6
7     7     7     8     8     8
9     9     9    10    10    10

```
```perms([2 4 6])

% nchoosek (MATLAB Function Reference) Binomial coefficient or all combinations
```
```ans =

6     4     2
6     2     4
4     6     2
4     2     6
2     4     6
2     6     4

```
```nchoosek(2:2:10,4)
```
```ans =

2     4     6     8
2     4     6    10
2     4     8    10
2     6     8    10
4     6     8    10

```

## And embedded set operations

```% intersect, ismember, issorted, setdiff, setxor, union, unique
```

http://strijov.com

```% this file: demo_principal_component
```