Most Frequent letter in a Matrix. Help Please!

Good night all,
I have a group of letters in a matrix, now I want to get a new matrix with the most frequent letter in groups of 4 rows.
So I have a matrix like this;
A
AB
AB
AB
B
B
B
B
(...)
and I want this;
AB
B
(...)
I'm reading the matrix with
[~,txt] = xlsread('Cópia de 2012_15min.xls','JAN','B25:B3000');
and I already tried
res = char( mode( double( reshape( txt, 4, [ ] ) ) ) )
but I'm getting an error
??? Error using ==> double Too many input arguments.
Error in ==> Untitled at 37 res = char( mode( double( txt, 4, [ ] ) ) )

 Accepted Answer

% Create sample data
letters = {...
'A'
'AB'
'AB'
'AB'
'B'
'B'
'B'
'B'}
% Now find unique letter pairs
uniqueLetters = unique(letters)

5 Comments

Thx for answering, the problem is that I need to find the most frequent letter in groups of 4 rows if I use that I will only get the unique letters in the matrix
So you need a histogram. I don't know if there's a histogram for strings. You'll probably need to take each unique string and go down your cell array counting matches. That will work. Please try it.
OK, this will work:
letters = {...
'A'
'AB'
'AB'
'AB'
'B'
'B'
'B'
'B'}
uniqueLetters = unique(letters)
histogram = zeros(1, length(uniqueLetters));
for ul = 1 : length(uniqueLetters)
fprintf('\nCounting %s...\n', uniqueLetters{ul});
histogram(ul) = sum(strcmp(uniqueLetters{ul}, letters) == 1)
end
histogram % Print to command window.
That's not making what I want maybe I was not clear. I have 2975 rows, in these rows I have letters like A B AB C, now I want to get a new matrix with the most frequent letters in groups of 4 rows.
So I have A A A A B B B C C C C D (...) and want a new matrix with A B C (...) so I will get a new matrix of letters with 744 rows
I'm now trying to use this code
[M,N] = size(txt); %# Matrix size
nSub = 744; %# Number of submatrices
novo = mat2cell(txt,diff(round(linspace(0,M,nSub+1))),N);
for i=1:744
[unique_strings, ~, string_map]=unique(novo{i})
most_common_string(i)=unique_strings(mode(string_map))
end
So I'm trying to split the matrix of 2975 rows into 744 new matrices of 4 rows, now I want to get the most frequent letter in each of the new matrices.
Sorry but I don't understand. You say you want "a new matrix of letters with 744 rows" and then you say want "744 new matrices of 4 rows" so I have no idea how many matrices you want (1 or 744) and how many rows (4 or 744) are supposed to be in it or them. And I don't know how you can find " the most frequent letter in each of the new matrices" if you don't count the letters (i.e. take the histogram like I did).
Hopefully someone else will understand you perfectly and supply you with a solution, because I can't.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!