Generate all possible patterns from an existent table

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

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

Thank you so much. That is what I was looking for; nevertheless, the PatternoutTable that I am looking for is the number of occurence that from each pattern from Pmtx. Ex: How many time [1 1 2 4 7] occured.
Thank you so much
My pleasure!
Add these lines to my previous code:
[RowPatterns,~,rowidx] = unique(Pmtx,'rows');
RowPatternSum = accumarray(rowidx, 1);
RowPatternChars = compose([repmat('%4d',1,size(RowPatterns,2))],RowPatterns);
RowPatternTable = table(RowPatternChars, RowPatternSum, 'VariableNames',{'Row_Pattern','Count'})
producing:
RowPatternTable =
7×2 table
Row_Pattern Count
________________________ _____
{' 1 1 2 4 7'} 1
{' 1 2 4 4 6'} 1
{' 1 2 6 8 8'} 1
{' 1 4 5 5 7'} 1
{' 2 3 3 5 5'} 1
{' 2 5 6 6 7'} 1
{' 3 4 5 7 8'} 1
The formatting is not perfect because of the necessity of providing for more than single-digit numbers in the patterns, however it should do what you requested. Adjust the formatting as necessary to produce the result you want.
Thank you so much. I don't think I would be able to come up with this.
Thank you.
Thank you so much.
As always, my pleasure!
I don't think I would be able to come up with this.’
Of course you would!
However you would likely need a bit more experience with MATLAB and its functions. I have those, you will learn them.
I appreciate. I will continue learning.
Would you explain a bit about the PL = [1:5:40; (5:5:40)+1E-12];
Thanks
The ‘PL’ assignment determines the limits for the comparison. The ‘1E-12’ term adds a very small amount to the upper value so that the comparison works efficiently, since it needs to compare the upper value with ‘less than’ not ‘less than or equal’ in order to make it work correctly.
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 Creating, Deleting, and Querying Graphics Objects 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!