how to get the most repeated element of a cell array?

i have an cell array like this
{[];[];[];[];[];[];[];[];'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';}
is there a way to get the label of this array as rj?

3 件のコメント

Cedric
Cedric 2013 年 4 月 26 日
Is the content either empty or equal to the same string? What happens if there are more empty cells than cells containing strings?
Matt J
Matt J 2013 年 4 月 26 日
maybe i did ask the question wrong, but if i have more {''} it will give me that name. instead i want {'rj'}. is there a workaround to counting?
Yes, you'll need to clarify the question. Why should {''} be ignored? Are there any other strings that should be ignored?
the cyclist
the cyclist 2013 年 4 月 26 日
In other words, you need to provide a more precise definition of the rule that defines the label.

サインインしてコメントする。

 採用された回答

Cedric
Cedric 2013 年 4 月 26 日
編集済み: Cedric 2013 年 4 月 26 日

0 投票

the cyclist >> I knew I had overlooked something easier. :-)
Well, look at how I did overlook something easier ;-D :
C = {[];[];[];[];[];[];[];[];'rj';'rj';'rj';'ab';'ab'} ;
setMatch = @(s,c) struct('string', s, 'count', c) ;
match = setMatch('', 0) ;
hashtable = java.util.Hashtable() ;
for k = 1 : length(C)
if isempty(C{k}), continue ; end
if hashtable.containsKey(C{k})
count = hashtable.get(C{k}) ;
if count >= match.count, match = setMatch(C{k}, count+1) ; end
hashtable.put(C{k}, count+1) ;
else
if match.count == 0, match = setMatch(C{k}, 1) ; end
hashtable.put(C{k}, 1) ;
end
end
Running this leads to;
>> match
match =
string: 'rj'
count: 3

5 件のコメント

Cedric
Cedric 2013 年 4 月 26 日
編集済み: Cedric 2013 年 4 月 26 日
Effess: I voted for the answer given by The Cyclist, because it is better. You can update it a little if you want to get rid of the empty cells, as follows: replace
cellData(indexToEmpty) = {''};
with
cellData(indexToEmpty) = [];
DuckDuck
DuckDuck 2013 年 4 月 26 日
supposed i have a matrix with these columns, how do i get another matrix with the results match for each column in separate column of match
Cedric
Cedric 2013 年 4 月 26 日
編集済み: Cedric 2013 年 4 月 26 日
If C is a "2D" cell array, yo can get column e.g. 4 as follows:
C_col = C(:,4) ;
The simplest way to apply either solution (The Cyclist or mine) to all columns is to loop over columns of your cell array, and to save the result in another cell array e.g.
matches = cell(1, size(C, 2)) ;
for c = 1 : size(C, 2)
C_col = C(:,c) ;
% .. whichever method, applied to C_col ..
matches{c} = .. the solution, e.g. match.string
end
DuckDuck
DuckDuck 2013 年 4 月 26 日
i did it but i'm geting an error when trying to save results from columns with more than one string!!
for k=1:length(tsf)
cellData = labelw(:,tsf(k));
indexToEmpty = cellfun(@isempty,cellData);
cellData(indexToEmpty) = [];
[uniqueCellData(:,k),~,whichCell] = unique(cellData);
end;
Cedric
Cedric 2013 年 4 月 26 日
編集済み: Cedric 2013 年 4 月 26 日
You don't need to save the content of temporary variables at each iteration of the loop. You just need to save results, and you should have something like (where the ".." have to be adapted to your case):
maxCountElements = cell(size(..), 1) ;
for k = 1 : ..
cellData = ..
indexToEmpty = cellfun(@isempty,cellData);
cellData(indexToEmpty) = [];
uniqueCellData = unique(cellData);
[uniqueCellData,~,whichCell] = unique(cellData);
cellCount = hist(whichCell,unique(whichCell));
[~,indexToMaxCellCount] = max(cellCount);
maxCountElements{k} = uniqueCellData(indexToMaxCellCount) ;
end

サインインしてコメントする。

その他の回答 (2 件)

the cyclist
the cyclist 2013 年 4 月 26 日
編集済み: the cyclist 2013 年 4 月 26 日

2 投票

Quite convoluted, but I think this works:
cellData = {[];[];[];[];[];[];[];[];'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';'rj';}
indexToEmpty = cellfun(@isempty,cellData);
cellData(indexToEmpty) = {''};
uniqueCellData = unique(cellData);
[~,whichCell] = ismember(cellData,unique(uniqueCellData))
cellCount = hist(whichCell,unique(whichCell));
[~,indexToMaxCellCount] = max(cellCount);
maxCountElement = uniqueCellData(indexToMaxCellCount)
The essence of the algorithm is using the hist() function to count up the frequency. Unfortunately, that function only works on numeric arrays, so I had to use the ismember() command to map the cell array values to numeric values.
A further complication was the existence of the empty cell elements. I replaced them with empty strings. You'll need to be careful if your original array has empty strings.

3 件のコメント

DuckDuck
DuckDuck 2013 年 4 月 26 日
maybe i did ask the question wrong, but if i have more {''} it will give me that name. instead i want {'rj'}. is there a workaround to counting?
Matt J
Matt J 2013 年 4 月 26 日
No need for ismember,
[uniqueCellData,~,whichCell] = unique(cellData);
the cyclist
the cyclist 2013 年 4 月 26 日
I knew I had overlooked something easier. :-)

サインインしてコメントする。

Peter Saxon
Peter Saxon 2021 年 1 月 23 日
編集済み: Peter Saxon 2021 年 1 月 23 日

0 投票

Found a neat solution with categories, just posting this here so when I forget how to do this and google it again I'll see it...
C = {[];[];[];[];[];[];[];[];'rj';'rj';'rj';'ab';'ab'} ;
catC=categorical(C);
catNames=categories(catC);
[~,ix] = max(countcats(catC));
disp(catName{ix}) % rj

カテゴリ

製品

質問済み:

2013 年 4 月 26 日

編集済み:

2021 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by