I am a newbie at matlab . As a part of a larger problem, I need to find maximum number of occurrences of a string in an array of Strings.
Since I have some experience in java I have written the partial code in java ( only until the number of occurences of the string within the string array can be computed I can sort the hashmap depending on the values and extract this)
int incr = 0;
String[] c = { "c1", "c2", "c3", "c1", "c2", "c2", "c2","c1","c2" };
Map<String, Integer> classRegistry = new HashMap<String, Integer>();
for (int i = 0; i < c.length; i++) {
String classes = c[i];
if (!(classRegistry.containsKey(classes))) {
for (int j = i + 1; j < c.length; j++) {
if (classes.equals(c[j])) {
incr++;
}
}
classRegistry.put(classes, incr+1);
incr = 0;
开发者_JS百科 }
}
Any idea how i can use something like a hashMap in MATLAB to calculate the number of occurrences of all the strings in an array
Thanks,
Bhavya
MATLAB has a function TABULATE available in the Statistics Toolbox:
c = {'c1' 'c2' 'c3' 'c1' 'c2' 'c2' 'c2' 'c1' 'c2'};
t = tabulate(c)
t = t(:,1:2)
The result:
t =
'c1' [3]
'c2' [5]
'c3' [1]
Alternatively, you can do the same using the UNIQUE and ACCUMARRAY functions:
c = {'c1' 'c2' 'c3' 'c1' 'c2' 'c2' 'c2' 'c1' 'c2'};
[classes,~,subs] = unique(c);
counts = accumarray(subs(:),1);
Again the result as before:
>> t = [classes(:) num2cell(counts)]
t =
'c1' [3]
'c2' [5]
'c3' [1]
Then to find the string that occurred the most, use:
>> [~,idx] = max(counts);
>> classes(idx)
ans =
'c2'
You didn't specify exaclty how you would want your input and output data types to be, but I wrote this quick script you might find usefull.
c = {'c1' 'c2' 'c3' 'c1' 'c2' 'c2' 'c2' 'c1' 'c2'};
count = struct();
for ic=1:length(c)
field = c{ic};
if isfield(count, field)
count = setfield(count, field, getfield(count, field) + 1);
else
count = setfield(count, field, 1);
end
end
The output for this specific c
would be
count =
c1: 3
c2: 5
c3: 1
Since you have experience with Java, you could just write your code in Java and call it from MATLAB. This techdoc article should help you get started if you decide to go this route. But it would probably be more useful to learn how to do it in m-script (see jomb87's answer)
Incidentally, you could improve the performance of your Java algorithm if you take further advantage of the hash map:
for (int i = 0; i < c.length; i++) {
String classes = c[i];
if (classRegistry.containsKey(classes)) {
classRegistry.put(classes, classRegistry.get(classes) + 1);
} else {
classRegistry.put(classes, 1);
}
}
精彩评论