Generate all possible patterns from an existent table

1 view (last 30 days)
Hello comunity,
ted learning matlab and I need your help to create a code that return all patterns using the data from a table. The dataset that I got from my test are like the following:
T =
[ 2 5 10 17 33
1 10 19 20 28
7 23 28 29 32
11 19 22 34 36
4 7 27 36 39
1 17 21 24 31
10 11 14 24 25]
The patterns should be based on variables of 5 numbers. Ex: P1= [1 2 3 4 5], P2 = [6 7 8 9 10] until 40. Applying it to the data from the table and using matlab the output to be something like:
P1, P1, P2, P4, P7
P1, P2, P4, P4, P6
P2, P5, P6, P6, P7
...
At the end, I should get another with a result of how many times each pattern occured. I can do all this using microsoft excel, however, whenever I got new data I have to do all over again because the new dataset maybe larger.
This how I was doing it:
%% Data extraction
Pick5 = readtable("Data_I.xlsx"); % Extract the excel file with all data
T = table2cell(Pick5) % convert the table to Cells
D1 = Pick5.Draw1; % Extract Column 1
D2 = Pick5.Draw2; % Extract Column 2
D3 = Pick5.Draw3; % Extract Column 3
D4 = Pick5.Draw4; % Extract Column 4
D5 = Pick5.Draw5; % Extract Column 5
Draw = [D1 D2 D3 D4 D5] %% Defining a new matrix using the previous (D's) Results
%% Pattern Generation (P-Pattern)
% Pat = sym('p', [1 8]); % create the pattern variables (ex: P1, P2 ...)
% pn = nchoosek([Pat],5); % create a symbolic matrix with the variables P1, P2 etc.
% Str = string(pn) % transfor the symbolic matrix to string
The thing is that this code works by first generaing all possible permutation which my cause me problem for situation where I have to run permutation of # over 11.
Thanks in advance

Accepted Answer

Star Strider
Star Strider on 7 Mar 2021
I am not certain what you want as a result.
One possibility:
T = [ 2 5 10 17 33
1 10 19 20 28
7 23 28 29 32
11 19 22 34 36
4 7 27 36 39
1 17 21 24 31
10 11 14 24 25];
PL = [1:5:40; (5:5:40)+1E-12]; % Pattern Limits
for k = 1:numel(T)
[~,Pv(k,:)] = find((T(k)>=PL(1,:)) & (T(k)<PL(2,:)),1); % Pattern Assignments (Rows Of ‘PL’ Corresponding To Elements Of ‘T’)
end
Pmtx = reshape(Pv,size(T,1),[]) % Matrix Of Patterns Corresponding To T’
PatternSum = accumarray(Pv, 1);
PatternOut = [PL.' PatternSum] % Frequencies Of Elements Within Specific Limits
PatternOutTable = array2table(PatternOut, 'VariableNames',{'Low_Limit','High_Limit','Count'})
producing:
Pmtx =
1 1 2 4 7
1 2 4 4 6
2 5 6 6 7
3 4 5 7 8
1 2 6 8 8
1 4 5 5 7
2 3 3 5 5
PatternOutTable =
8×3 table
Low_Limit High_Limit Count
_________ __________ _____
1 5 5
6 10 5
11 15 3
16 20 5
21 25 6
26 30 4
31 35 4
Adapt those results to produce the output that you want.
  8 Comments
Star Strider
Star Strider on 8 Mar 2021
As always, my pleasure!
Actually, that was left over from an earlier experiment using only a vector for ‘PL’, rather than the matrix.
This would also work:
PL = [1:5:40; (5:5:40)]; % Pattern Limits
for k = 1:numel(T)
[~,Pv(k,:)] = find((T(k)>=PL(1,:)) & (T(k)<=PL(2,:)),1); % Pattern Assignments (Rows Of ‘PL’ Corresponding To Elements Of ‘T’)
end
.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!