How to count the each pair of sequence apeared

1 view (last 30 days)
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

Answers (1)

Image Analyst
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
Mekala balaji
Mekala balaji on 15 Aug 2015
Sir, I don't mind B appeared after A or after B itself (like at row3). I just want to count how it appeared (that is after A or after B itself) but how many time B appeared after A, and B appeared after B it self. Same as for A. That's why I want to count as below matrix: The combination matrix is as below (as we knew):
AA AB
BA BB
As I said before, AA means A after A, AB means B after A, BA means A after B, and BB means B after B. I want to check and count how many times we have these sequences. My resultant count matrix is as below:
Resultant matrix(of count)
1 2
1 1
AA count =1, because A appeared after A only once at row5. AB count is 2, it means B appeared after A twice at row2 and row6. BA count is 1 because A appeared after B only once at row4. Finally, BB count is 1, since B appeared after B only once at row3.
Image Analyst
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

Sign in to comment.

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!