How to count the each pair of sequence apeared
1 view (last 30 days)
Show older comments
Hi, I have below array (of three columns).I want to count how many times B appeared after A (or B) or A appeared after B (or A). Here I only shown A&B combination, but actually I have A~Z combinations(26by26 matrix).Kindly help me, many thanks in advance.
A
B
B
A
A
B
I
0 Comments
Answers (1)
Image Analyst
on 14 Aug 2015
Edited: Image Analyst
on 14 Aug 2015
Try this:
% Create sample data.
m= ['A','B','C','A','B';'Q','A','B','E','R';'A','B','A','B','B']
[rows, columns] = size(m);
for row = 1 : rows
% Find the locations and print them out.
loc = strfind(m(row,:), 'AB');
fprintf('\nFor row %d, AB occurs %d times, at these locations: ', ...
row, length(loc));
fprintf('%d ', loc);
end
fprintf('\n'); % Add a line feed after all the numbers.
In the command window:
m =
ABCAB
QABER
ABABB
For row 1, AB occurs 2 times, at these locations: 1 4
For row 2, AB occurs 1 times, at these locations: 2
For row 3, AB occurs 2 times, at these locations: 1 3
[EDIT]
If you have the Image Processing Toolbox, you can do this for all letters in a single line of code if you use the function graycomatrix().
6 Comments
Image Analyst
on 15 Aug 2015
If I understood you correctly, this should do it:
% Create sample data.
rows = 200;
columns = 2;
% Make array, of 2 columns, with random letters.
m = char(randi([1,26], rows, columns) + 'A' - 1);
[rows, columns] = size(m);
counts = zeros(26, 26);
for col = 1 : columns % For columns 1 and 2...
% Extract only this row.
thisColumn = m(:, col);
for c1 = 1 : 26 % for every character
thisChar = c1 + 'A' - 1;
% Find first instance of this letter in the column
firstIndex = strfind(thisColumn', thisChar);
if isempty(firstIndex)
% Character is not in this row.
fprintf('%c not found in column %d\n', thisChar, col);
% Skip to the next character
continue;
end
% Now extract the column from this point down,
% Because we only want to consider how many times
% a letter occured AFTER this index, not before.
subColumn = thisColumn(firstIndex:end);
for c2 = 1 : 26 % for every character
otherChar = c2 + 'A' - 1;
% Get a logical vector of where otherChar occurs
locations = strfind(subColumn', otherChar);
if ~isempty(locations)
% If the other character appears
% count the number of times and assign it.
numTimes = length(locations);
counts(c1, c2) = numTimes;
fprintf('%c appears %d times after %c in column %d\n', otherChar, numTimes, thisChar, col);
end
end
end
end
counts % Echo to command window
See Also
Categories
Find more on Computer Vision Toolbox 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!